I am pretty sure there is a (very) simple explanation for the error that I am seeing but I cannot seem to figure out what it is. So, without much further ado:
1.) Code:
//main.cpp
include <cstdlib>
include <iostream>
include "mytest.cpp"
using namespace std;
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}
//mytest.cpp
ifndef ach_MYTEST_CPP_
define ach_MYTEST_CPP_
class mytest
{
public:
mytest();
~mytest();
private:
void Testing();
};
mytest.o(.text+0x0):mytest.cpp: multiple definition of `mytest::Testing()'
main.o(.text+0x100):main.cpp: first defined here
collect2: ld returned 1 exit status
make.exe: *** [TestProjects.exe] Error 1
Execution terminated
3.) Clearly I have not correctly defined/declared "Testing" - but this is exactly the way many tutorials/books do it. Defining Testing inline solves the problem however.
I would appreciate any help
Regards,
ACH
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-08-31
You have both #included mytest.cpp and separately compiled and linked it.
include is a simple textual replacement so by including mytest.cpp in main.cpp you have essentially defined class mytest in main.cpp (and it is compiled into main.o). You have additionally separately compiled mytest.cpp and linked it, so mytest.o has a definition of calss mytest and so does main.o.
You should have two separate files mytest.h and mytest.cpp, and include mytest.h in both main.cpp and mytest.cpp:
//mytest.h
ifndef ach_MYTEST_H_
define ach_MYTEST_H_
class mytest
{
public:
mytest();
~mytest();
private:
void Testing();
};
endif //ach_MYTEST_H_
//mytest.cpp
include "mytest.h"
void mytest::Testing()
{
//any code here;
}
// don't forget the constructor destructor as well!
Basically you never #include a .cpp file, and .h files should contain only declarations (as opposed to definitions) or in-line code.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am pretty sure there is a (very) simple explanation for the error that I am seeing but I cannot seem to figure out what it is. So, without much further ado:
1.) Code:
//main.cpp
include <cstdlib>
include <iostream>
include "mytest.cpp"
using namespace std;
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}
//mytest.cpp
ifndef ach_MYTEST_CPP_
define ach_MYTEST_CPP_
class mytest
{
public:
mytest();
~mytest();
private:
void Testing();
};
void mytest::Testing()
{
//any code here;
}
endif //ach_MYTEST_CPP_
2.) Compile log:
ompiler: Default compiler
Building Makefile: "D:\DevCpp\MyProjects\TestProjects\Makefile.win"
Executing make...
make.exe -f "D:\DevCpp\MyProjects\TestProjects\Makefile.win" all
g++.exe -c mytest.cpp -o mytest.o -I"D:/DevCpp/lib/gcc/mingw32/3.4.2/include" -I"D:/DevCpp/include/c++/3.4.2/backward" -I"D:/DevCpp/include/c++/3.4.2/mingw32" -I"D:/DevCpp/include/c++/3.4.2" -I"D:/DevCpp/include"
g++.exe main.o mytest.o -o "TestProjects.exe" -L"D:/DevCpp/lib"
mytest.o(.text+0x0):mytest.cpp: multiple definition of `mytest::Testing()'
main.o(.text+0x100):main.cpp: first defined here
collect2: ld returned 1 exit status
make.exe: *** [TestProjects.exe] Error 1
Execution terminated
3.) Clearly I have not correctly defined/declared "Testing" - but this is exactly the way many tutorials/books do it. Defining Testing inline solves the problem however.
I would appreciate any help
Regards,
ACH
Thank You. That was crystal clear.
ACH
You have both #included mytest.cpp and separately compiled and linked it.
include is a simple textual replacement so by including mytest.cpp in main.cpp you have essentially defined class mytest in main.cpp (and it is compiled into main.o). You have additionally separately compiled mytest.cpp and linked it, so mytest.o has a definition of calss mytest and so does main.o.
You should have two separate files mytest.h and mytest.cpp, and include mytest.h in both main.cpp and mytest.cpp:
//mytest.h
ifndef ach_MYTEST_H_
define ach_MYTEST_H_
class mytest
{
public:
mytest();
~mytest();
private:
void Testing();
};
endif //ach_MYTEST_H_
//mytest.cpp
include "mytest.h"
void mytest::Testing()
{
//any code here;
}
// don't forget the constructor destructor as well!
Basically you never #include a .cpp file, and .h files should contain only declarations (as opposed to definitions) or in-line code.
Clifford