+ 1
can i write function definitions in header files?
i just cut all my functions in main.c and pasted them in a header file to make it look cleaner and included that in main.c, now its not compiling what should i do instead
3 Antworten
+ 2
You can do that, but you will need another source file too. For example:
func.h
void printHello();
func.cpp
void printHello() {
cout << "Hello!\n";
}
And then in main.cpp
#include "func.h";
...
printHello();
...
Whenever I have functions that I don't want filling up my main file, I'll put them in separate files like that and I usually call them Utility.h and Utility.cpp. I'll also wrap the file in a namespace so I know where they came from.
func.h
namespace Utility {
void printHello();
}
func.cpp
void Utility::printHello() { };
+ 2
Yes the have to
+ 1
does the .h and .cpp files need to have the same name