I don't get why it is not compiling

So, basically i'm trying to make a program, but that is not the point. It should work, i'm sure. But i don't get why:

1
2
3
4
5
6
7
8
class Hi{
public:
    Hi();
};

Hi::Hi(){
}


The Errors:
1
2
3
4
5
6
7
8
9
||=== Build: Debug in TheDbDESKOP (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `ZN2HiC2Ev':|
C:\TheDbDESKOP\Class.cpp|6|multiple definition of `Hi::Hi()'|
obj\Debug\Class.o:C:\TheDbDESKOP\Class.cpp|6|first defined here|
obj\Debug\main.o||In function `ZN2HiC2Ev':|
C:\TheDbDESKOP\Class.cpp|6|multiple definition of `Hi::Hi()'|
obj\Debug\Class.o:C:\TheDbDESKOP\Class.cpp|6|first defined here|
||error: ld returned 1 exit status|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


I should include my main also:
1
2
3
4
5
6
7
#include "Class.cpp"

int main(int argc, char *argv[])
{

return 0;
}
Last edited on
Do not include source files (.cpp), but headers (.h / .hpp), where you only declare (but do not define) the class, function or whatever. So, divide your code in such a manner:

class.h
1
2
3
4
class hi {
public: 
void hi();
};


class.cpp
void hi::hi() { ... }

main.cpp
1
2
#include "class.h"
// the rest 
Last edited on
From your error messages, it looks like snippet #1 is a .cpp file (you didn't identify the filename).

Snippet #3, line 1: Never #include a .cpp file. This will usually result in multiply defined symbols as you error messages indicate.

You've also included class.cpp in you project. See snippet #2 line 4. The combination of Snippet #3, line 1 and including class.cpp in your project caused the function definition to be compiled twice which is why the loader is complaining.

You need to split snippet #1 into a .h file (lines 1-4) and a .cpp file (lines 5-8) and change snippet #3 to #include "hi.h"

Hello xx1182,

I believe what is happening here is that "Class.cpp" is being compiled first and the when you include "Class.cpp" in main the compiler is trying to compile the same code a second time. Thus causing your errors.

What I see as being most often done is to put the class definition in say "Hi.h" and the function definitions in "Hi.cpp. Then in main you include "Hi.h". This way if "Hi.cpp is compiles first it will only be done once.

I would refrain from using "Class" as a file name as it does not describe what the file is about. I tend to use the class or function name as the file name. It makes it easier to know what is in the file.

Try this and see if it makes a difference:

Header file Hi.h
1
2
3
4
5
6
class Hi
{
    public:
        Hi();
        ~Hi();
};


.cpp file HI.cpp
1
2
3
4
#include "Hi.h"

Hi::Hi(){}
Hi::~HI(){}


main.cpp file
1
2
3
4
5
6
7
8
#include <iostream>  // <--- Includes other header file you do not see, but may be useful.
#include "Hi.h"

int main(int argc, char *argv[])  // <--- If you do not use the parameters they are not needed here.
{

    return 0;
}


Hope that helps,

Andy
Topic archived. No new replies allowed.