Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator overloading c++ / where to put my code?

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

like image 987
ftiaronsem Avatar asked Mar 27 '11 14:03

ftiaronsem


People also ask

How do you write operator overloading?

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.

Can we do operator overloading in C?

C does not support operator overloading (beyond what it built into the language).

How do you declare an operator function?

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.

What is operator overloading and asked to write the code of it?

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 +.

How to overload an operator in C?

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.

What are the different types of operator overloading?

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...

How do you know if an operator is overloaded?

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.

How to overload the binary operator for user-defined types?

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.


2 Answers

The compiler must see the implementation to be able to use the template. Usually that means you put it in the header.

like image 57
Bo Persson Avatar answered Sep 28 '22 10:09

Bo Persson


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);
like image 41
pajton Avatar answered Sep 28 '22 10:09

pajton