Today I overloaded the << operator in one of my classes:
#ifndef TERMINALLOG_HH
#define TERMINALLOG_HH
using namespace std;
class Terminallog {
public:
Terminallog();
Terminallog(int);
virtual ~Terminallog();
template <class T>
Terminallog &operator<<(const T &v);
private:
};
#endif
As you can see I defined the overloaded operator in my header file and I went on implementing it in my .cc file:
//stripped code
template <class T>
Terminallog &Terminallog::operator<<(const T &v) {
cout << endl;
this->indent();
cout << v;
return *this;
}
//stripped code
Afterwards I created a main.cpp file using my new class:
#include "inc/terminallog.hh"
int main() {
Terminallog clog(3);
clog << "bla";
clog << "bla";
return 0;
}
and i went on compilying:
g++ src/terminallog.cc inc/terminallog.hh testmain.cpp -o test -Wall -Werror
/tmp/cckCmxai.o: In function `main':
testmain.cpp:(.text+0x1ca): undefined reference to `Terminallog& Terminallog::operator<< <char [4]>(char const (&) [4])'
testmain.cpp:(.text+0x1de): undefined reference to `Terminallog& Terminallog::operator<< <char [4]>(char const (&) [4])'
collect2: ld returned 1 exit status
BAM! a stupid linker error and I still have no idea where it comes from. I played around a bit and noticed that putting the implementation of my overloaded operator in my header file solves all problems. Now I am even more confused than before.
Why can't I put the implementation of the overloaded operator in my .cc file? Why is it running smoothly when I put it in my header file?
Confused thanks in advance
ftiaronsem
Operator Overloading in Binary Operators Here, + is a binary operator that works on the operands num and 9 . When we overload the binary operator for user-defined types by using the code: obj3 = obj1 + obj2; The operator function is called using the obj1 object and obj2 is passed as an argument to the function.
C does not support operator overloading (beyond what it built into the language).
You declare an operator function with the keyword operator preceding the operator. Overloaded operators are distinct from overloaded functions, but like overloaded functions, they are distinguished by the number and types of operands used with the operator. Consider the standard + (plus) operator.
This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.
An operator can be overloaded by defining a function to it. The function of the operator is declared by using the operator keyword. Syntax : Note : Operator overloading is basically the mechanism of providing a special meaning to an ideal C# operator w.r.t. a user-defined data type such as structures or classes.
1 For operator overloading to work, at least one of the operands must be a user defined class object. 2 Assignment Operator: Compiler automatically creates a default assignment operator with every class. ... 3 Conversion Operator: We can also write conversion operators that can be used to convert one type to another type. ... More items...
The operator keyword followed by the operators symbol tells us which operator is being overloaded. We also have a display function to allow us to see the display of the object's member values. We will substituted this with the overloaded operator ( <<) later in the post.
When we overload the binary operator for user-defined types by using the code: The operator function is called using the obj1 object and obj2 is passed as an argument to the function. using & makes our code efficient by referencing the complex2 object instead of making a duplicate object inside the operator function.
The compiler must see the implementation to be able to use the template. Usually that means you put it in the header.
It is possible to keep implementation in cpp file, but you need to declare usage of your template for every type you are using it with. Please see Parashift C++ Faq for more detailed explanation.
In your case, you have to write that line somewhere in your cpp file:
template Terminallog &Terminallog::operator<<(const char* &v);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With