Skip to main content
gamedev.net
🔒 Locked

multiple definition errors in dev c++

Started by Qually Jun 1, 2003 at 3:12 PM 1 replies 750+ views
Original Post
Qually
Qually
I use dev-c++ 4, and I keep getting multiple definition errors whenever I try to compile programs. I have a class called Powerball, and it is in a file called "powerball.cpp". I have the necessary #ifndef _POWERBALL_CPP #define _POWERBALL_CPP #endif but when powerball.cpp is added to my project I get "multiple definition of Powerballowerball(void)" errors for every function in the class. I #include "powerball.cpp" in main, and when I remove "powerball.cpp" from the project, it compiles fine. Does anyone know what the problem is?
kieren_j
kieren_j
Usually, only include files are #included
Add source files to your project (dont know how in Dev-C++...) containing implementations and #include header files where necessary, containing definitions.

i.e.: Project contains main.cpp, powerball.cpp. main.cpp has #include "powerball.h", a file which contains definitions (but not implementations) for powerball.cpp. That way main.cpp scope can access powerball.cpp stuff.

Note, remember if you want to access global variables from powerball.cpp in main.cpp, use extern. i.e. in powerball.cpp: int powerball_global; and in powerball.h: extern int powerball_global;

out
Qw3r7yU10p!
Qw3r7yU10p!
you''ve got it the wrong way around

the cpp files are what is compiled by the compiler. It will read the file once.

the header files are included in lots of different cpp files and in other header files too. these are the ones you want to protect from being included lots of times.

put the #ifndef etc. in the headers. then after the header has been included once in the cpp file it won''t be included again.

pete

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.