I’ve replicated the problem into their simpler versions.
The command g++ -fdiagnostics-color=always -g -o test.exe *.cpp
is ran in a windows terminal.
The result is instead multiple definition errors which refer to a definition within the same line and file.
Sample mutliple definition error (there’s multiple, but generally says the same issue with a different method):
C:/ToProject/File.tpp:11: multiple definition of `method signature'; C:ToTempFiletempfile.o:C:/ToProject/File.tpp:11: first defined here
Project:
./
ClassA.h
ClassA.tpp
ClassB.h
ClassB.cpp
test.cpp
ClassA.h
#ifndef CLASSA_H
#define CLASSA_H
#include <cstdint>
#include <list>
class classA_A{
protected:
uint32_t value;
public:
classA_A(uint32_t value = 0x7);
};
class classA_B{
public:
classA_B(classA_A* value = nullptr);
protected:
classA_A* value;
};
template <typename X>
class classA_C: public classA_A{
public:
classA_C();
protected:
std::list<X>* list;
};
#include "ClassA.tpp"
#endif // CLASSA_H
ClassA.tpp
#ifndef ClassA_TPP
#define ClassA_TPP
#include "ClassA.h"
using namespace std;
classA_A::classA_A(uint32_t value){
this->value = value;
}
classA_B::classA_B(classA_A* value){
this->value = value;
}
template<typename X>
classA_C<X>::classA_C():
classA_A(0x9){
list = new std::list<X>();
}
#endif
ClassB.h
#ifndef CLASSB_H
#define CLASSB_H
#include "ClassA.h"
class classB_A: public classA_B{
public:
uint32_t get_v()const;
};
class classB_B: public classA_C<uint32_t>{
public:
classB_B();
};
#endif // CLASSB_H
ClassB.cpp
#include "ClassB.h"
using namespace std;
uint32_t classB_A::get_v() const {
return 0x3;
}
classB_B::classB_B():
classA_C<uint32_t>() {
}
test.cpp
#include <iostream>
#include "ClassA.h"
using namespace std;
int main() {
classA_C<uint32_t>* thing = new classA_C<uint32_t>();
delete thing;
return 0;
}
As above, I was expecting to try to use the command and successfully run the command without error.
I tried using an include guard on ClassA.tpp, moved code from ClassA.tpp to ClassA.h. The first results in an the same error (the code above is the earliest) and the second results in the same error but rewritten to the respective file.
CHL_a is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.