My third question here today ;-), but I am really new to c++ template programming and operator overloading.
I am trying the following:
terminallog.hh
//snipped code
class Terminallog {
public:
Terminallog();
Terminallog(int);
virtual ~Terminallog();
template <class T>
Terminallog & operator<<(const T &v);
template <class T>
Terminallog & operator<<(const std::vector<T> &v);
template <class T>
Terminallog & operator<<(const T v[]);
Terminallog & operator<<(const char v[]);
//snipped code
};
//snipped code
template <class T>
Terminallog &Terminallog::operator<<(const T &v) {
std::cout << std::endl;
this->indent();
std::cout << v;
return *this;
}
template <class T>
Terminallog &Terminallog::operator<<(const std::vector<T> &v) {
for (unsigned int i = 0; i < v.size(); i++) {
std::cout << std::endl;
this->indent();
std::cout << "Element " << i << ": " << v.at(i);
}
return *this;
}
template <class T>
Terminallog &Terminallog::operator<<(const T v[]) {
unsigned int elements = sizeof (v) / sizeof (v[0]);
for (unsigned int i = 0; i < elements; i++) {
std::cout << std::endl;
this->indent();
std::cout << "Element " << i << ": " << v[i];
}
return *this;
}
Terminallog &Terminallog::operator<<(const char v[]) {
std::cout << std::endl;
this->indent();
std::cout << v;
return *this;
}
//snipped code
Trying to compile this code, it gives me a linker error:
g++ src/terminallog.cc inc/terminallog.hh testmain.cpp -o test -Wall -Werror
/tmp/ccify开发者_开发技巧Opr.o: In function `Terminallog::operator<<(char const*)':
testmain.cpp:(.text+0x0): multiple definition of `Terminallog::operator<<(char const*)'
/tmp/cccnEZlA.o:terminallog.cc:(.text+0x0): first defined here
collect2: ld returned 1 exit status
Therfore I am currently banging my head against the wall, searching for a solution. But I can't find one in the wall...
It would be great if someone could show me my mistake
Thank you so much
ftiaronsem
This function
Terminallog &Terminallog::operator<<(const char v[]) {
std::cout << std::endl;
this->indent();
std::cout << v;
return *this;
}
is not a template, but a regular member function. If this .h file is included in several .cpp filtes, it will cause the multiple definition error you're seeing. If you declare it inline
, the compiler will allow the multiple definitions and your error should be fixed.
inline
Terminallog &Terminallog::operator<<(const char v[]) {
精彩评论