Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i compile my source code ""g++ main.cpp Test.cpp", i have met error:
"/tmp/ccSer90m.o: (.bss+0x0): multiple definition of `Test::count'
/tmp/cc7iaBlq.o: (.bss+0x0): first defined here
collect2: ld returned 1 exit status"

File "Test.h":
C++
class Test
{
public:
        Test();
private:
        static int count;
};
int Test::count = 0;

File "Test.cpp":
C++
#include <stdio.h>
#include <stdlib.h>
#include "Test.h"
using namespace std;
Test::Test()
{
        count++;
        cout<<count<<endl;
}

File "main.cpp"
C++
#include <stdio.h>
#include <stdlib.h>
#include "Test.h"
using namespace std;

int main ( int argc, char *argv[] )
{
        Test* t = new Test();
        return EXIT_SUCCESS;
}  


Please solve this error help me, thanks!
Posted

The definition (int Test::count = 0;) must be put in a .cpp file and not in a header file.
 
Share this answer
 
Try moving this line:
int Test::count = 0;


from the .h file to the .cpp file.

The problem is that each time you include "Test.h" the count variable gets defined - hence multiple definitions. Moving the line to the .cpp file ensures that there is only one definition of count.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900