header-files

Header file and extern keyword

a 夏天 提交于 2019-12-02 05:15:39
问题 I am having a lot of issue using extern variable and header files. I have read through sections of books and searched the web for hours but I haven't been able to figure out. Any help in understanding this problem will be greatly appreciated. The following is the code and the error when I try to compile #include <stdio.h> #include <stdlib.h> #include <string.h> #include "sample.h" int main() { int i; int gI = 0; double recv; i = 10; gI = i; recv = AnotherFunc(); return 0; } And the sample.h

Defining a member function inside the class instead of outside in C++?

二次信任 提交于 2019-12-02 04:37:50
By writing the definition of a member function inside the class in the header file as follows, I was told that compiler treats this as an inline function - hence , anywhere it encounters this function being called in the program , it enters the complete body of the function every time instead of jumping to the one location where the function resides in memory. Is this true? class myClass { public: void Fun1() { //implemented here } } I was told that compiler treats this as an inline function Correct. That means that it's allowed to be defined in more than one translation unit, which is

C program cannot find function which included in header file

只谈情不闲聊 提交于 2019-12-02 04:36:33
I made program like this. 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include "libavformat/avformat.h" 5 6 int main (int argc, char* argv[]){ 7 av_register_all(); 8 return 0; 9 } My header file located in root@ubuntu:/home/juneyoungoh/getDuration# find / -name "avformat.h" /root/ffmpeg/libavformat/avformat.h /usr/local/include/libavformat/avformat.h then I run with gcc getDuration.c , but I show message like below. root@ubuntu:/home/juneyoungoh/getDuration# gcc getDuration.c /tmp/ccwjonqH.o: In function `main': getDuration.c:(.text+0x10): undefined reference to `av

Declaring a function that return a 2D array in a header file?

女生的网名这么多〃 提交于 2019-12-02 03:50:12
I am trying to declare, within my header file, a function that returns a 2D array. How can this be accomplished, given that we already know the size of the array? Below is what I'm currently doing. class Sample { public: char[x][y] getArr(); void blah(int x, int y); private: const static int x = 8; const static int y = 2; char arr[x][y]; }; chrisaycock It turns-out my original answer was totally incorrect, but I can't delete it since it's been accepted . From two separate answers below, I was able to compile this: class Sample { const static int x = 8; const static int y = 2; public: typedef

Header file and extern keyword

ぃ、小莉子 提交于 2019-12-02 00:54:22
I am having a lot of issue using extern variable and header files. I have read through sections of books and searched the web for hours but I haven't been able to figure out. Any help in understanding this problem will be greatly appreciated. The following is the code and the error when I try to compile #include <stdio.h> #include <stdlib.h> #include <string.h> #include "sample.h" int main() { int i; int gI = 0; double recv; i = 10; gI = i; recv = AnotherFunc(); return 0; } And the sample.h is the following #ifndef SAMPLE #define SAMPLE extern int gI; extern double AnotherFunc(); #endif And

Inclusion of header files in case of templates.

蹲街弑〆低调 提交于 2019-12-02 00:31:22
When we make a class we declare its functions in a header files and define them in a source file... then the header file can be included in the main file to use the class... But if we declare a template class in header files and define it in the .cpp file and then if we include the header file in the main(containing int main) file then why does a linker error crop up... and the error does not crop up if we included the .cpp file(containing the header file ) in the main file... any answers plz? Templates don't actually produce any object code at the point where the compiler reads their source

Including headers and Main.h [closed]

孤街浪徒 提交于 2019-12-01 21:56:14
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Ok not sure if this is the right way or even a correct way but i have seen this and started to use it, Say you have 6 files main.cpp main.h car.cpp car.h speed.cpp speed.h 1st - should you ever have a main.h? 2nd - if main.h has #include car.h and #include speed.h then in car/speed.cpp do you just have to add

Missing ';' before 'template<'

醉酒当歌 提交于 2019-12-01 21:43:47
I'm getting a strange error when I'm compiling my program: Error 1 error C2143: syntax error : missing ';' before ''template<'' I'm doing everything pretty standard; nothing out of the ordinary: #ifndef HEAP_H #define HEAP_H //************************************************************************** template<typename TYPE> class Heap { private: TYPE* heapData; int currSize; int capacity; void _siftUp(int); void _siftDown(int); int _leftChildOf(int) const; int _parentOf(int) const; public: Heap(int c = 100); ~Heap(); bool viewMax(TYPE&) const; int getCapacity() const; int getCurrSize() const;

Is it possible to reference C enums from an assembly file?

馋奶兔 提交于 2019-12-01 20:57:23
问题 syscalls.h enum Syscall { OPEN_FILE, READ_FILE, CLOSE_FILE }; syscalls.s extern WRITE_TO_SCREEN global write_to_screen write_to_screen: mov eax, WRITE_TO_SCREEN mov ebx, [esp+4] int 0x80 ret Gives me this error: stdlib/syscalls.o: In function `write_to_screen': stdlib/syscalls.s:(.text+0x1): undefined reference to `WRITE_TO_SCREEN' make: *** [kernel.elf] Error 1 回答1: No there isn't. You could do something like: enum_support.h #ifdef __ASSEMBLER__ #define ENUM_START #define ENUM_VALUE(key

class foo; in header file

孤人 提交于 2019-12-01 20:56:08
Is some one able to explain why header files have something like this? class foo; // This here? class bar { bar(); }; Do you need an include statement when using this? Thanks. The first class foo; is called a forward declaration of the class foo. It simply lets the compiler know that it exists and that it names a class. This makes foo what is called an "incomplete type" (unless the full declaration of foo has already been seen). With an incomplete type, you can declare pointers of that type, but you cannot allocate instances of that type or do anything that requires knowing its size or members