My project won't compile if I declare a class in another file
6 replies, posted
Language: C++
IDE: Code::Blocks
For ease of use and not having one big and cluttered main file, I like to separate all my helper functions into another file and then include that into the main file. However, I don't know how to do it properly with header files, so I just include it as a .cpp file. Here's an example:
main.cpp
[code]
#include "functions.cpp"
int main()
{
car minivan(20, 30, 0.5);
}
[/code]
functions.cpp
[code]
class car
{
function's and variables here
};
car::car()
{
constructor here
}
[/code]
As improper as this may be, I've done this before in other IDE's such as Dev C++ and it works fine. With Code::Blocks however, it generates no .exe file when I build. There are no errors; everything appears to compile fine. Could someone please explain why this isn't working and how to get it working.
Does your code actually say 'function's and variables here' you know you can't do that put // infront of it.
class car
{
function's and variables here
};
No protoype :X
car::car() -> car minivan(20, 30, 0.5); !?
[url]www.learncpp.com[/url]
I know classes, I was just writing some pseudocode. Anyway, it compiles and works if I put it all in one file.
Don't include cpp files. Learn how to do it properly.
What does the code::blocks output say? It's useless to write a thread about it if you don't show us the error or the output.
The output is the same as if I had put it in one file. No errors and the same warnings. The full output is:
[code]
C:\Users\George\Desktop\My Stuff\Files\Code And Programs\SFML Programs\Sprite Test\main.cpp||In function `int main()':|
C:\Users\George\Desktop\My Stuff\Files\Code And Programs\SFML Programs\Sprite Test\main.cpp|80|warning: unused variable 'ElapsedTime'|
||Info: resolving sf::Font::ourDefaultCharset by linking to __imp___ZN2sf4Font17ourDefaultCharsetE |
||Info: resolving vtable for sf::Shapeby linking to __imp___ZTVN2sf5ShapeE |
||Info: resolving vtable for sf::Stringby linking to __imp___ZTVN2sf6StringE |
||Info: resolving vtable for sf::Drawableby linking to __imp___ZTVN2sf8DrawableE |
||Info: resolving vtable for sf::Spriteby linking to __imp___ZTVN2sf6SpriteE |
||warning: auto-importing has been activated without --enable-auto-import specified on the command line.|
||=== Build finished: 0 errors, 2 warnings ===|
[/code]
Really you should have a header and code file for the car class:
car.h (try to name your class files similar to the class)
[cpp]
class car
{
public:
// declare public variables and functions
car();
car(int a, int b, float c) // you are calling the constructor with 3 variables in your code, so heres the extra constructor
private:
// declare private variables and functions
};
[/cpp]
in car.cpp:
[cpp]
car::car()
{
//constructing
}
car::car(int a, int b, float c)
{
// the extra constructor with arguments
}
[/cpp]
in main.cpp:
[cpp]
#include "car.h"
int main()
{
car minivan(20, 30, 0.5);
return 0; // return an int :)
}
[/cpp]
That should work, it may seem a bit much to declare these functions in one file and define them in another, but when you start creating more complex classes you'll realise how much neater and easier it is to work with.
Thanks, I never really knew how to properly use header files. By the way, the code I posted was a complete example; something just to illustrate a point.
Sorry, you need to Log In to post a reply to this thread.