exer240
exer240

Reputation: 37

Trying to overload operator<< in template class

(this is an old exam for my current course)

I'm trying to overload operator<< in a template class but when I compile the code and run the program i get this message:

/usr/bin/ld: /tmp/cc2BvB2u.o: in function `main':
/home/user/Skola/TDDI82/TENTA2017_1/UPPGIFT4/main.cpp:30: undefined reference to `operator<<(std::ostream&, Wrapper<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
/usr/bin/ld: /home/user/Skola/TDDI82/TENTA2017_1/UPPGIFT4/main.cpp:31: undefined reference to `operator<<(std::ostream&, Wrapper<int> const&)'
/usr/bin/ld: /home/user/Skola/TDDI82/TENTA2017_1/UPPGIFT4/main.cpp:32: undefined reference to `operator<<(std::ostream&, Wrapper<int> const&)'
collect2: error: ld returned 1 exit status

All the other tests (my cout:s) are acting as I would expect. Down below here I've put all my code for this program. When I have used friend for overload of operator<< this hasn't been a problem. Anyone knows what wrong I'm doing and how to solve it?

//wrapper.h

#ifndef WRAPPER_H
#define WRAPPER_H

#include <ostream>

template<typename T>
class Wrapper
{
public:

    Wrapper();
    Wrapper(T const&);
    Wrapper(Wrapper const&);

    T get_value() const;
    void set_value(T const& v);

    Wrapper& operator=(T const& rhs);
    
    friend std::ostream& operator<<(std::ostream& os, typename Wrapper<T>::Wrapper const& w);
private:
    T value;
};

template<typename T>
std::ostream& operator<<(std::ostream& os, typename Wrapper<T>::Wrapper const& w);


#include "wrapper.tcc"

#endif

//wrapper.tcc

#ifndef WRAPPER_TCC
#define WRAPPER_TCC

#include "wrapper.h"

template<typename T>
Wrapper<T>::Wrapper()
    :  value{}
    {}

template<typename T>
Wrapper<T>::Wrapper(T const& t)
    :   value{t}
    {}

template<typename T>
Wrapper<T>::Wrapper(Wrapper const& w)
{
    *this = w;
}

template<typename T>
T Wrapper<T>::get_value() const
{
    return value;
}

template<typename T>
void Wrapper<T>::set_value(T const& v) 
{
    value = v;
}

template<typename T>
typename Wrapper<T>::Wrapper& Wrapper<T>::operator=(T const& rhs)
{
    value = rhs;
    return *this;
}

template<typename T>
std::ostream& operator<<(std::ostream& os, typename Wrapper<T>::Wrapper const& w)
{
    os << w.get_value();
    return os;
}

#endif

//main.cpp

#include "wrapper.h"
#include <stream>
#include <string>

int main()
{
    //test
    Wrapper<std::string> w1{"hej"};
    Wrapper<int> w2{3};
    Wrapper<int> w3{w2};
    std::cout << w1.get_value() << std::endl;
    std::cout << w2.get_value() << std::endl;
    std::cout << w3.get_value() << std::endl;

    w1.set_value("nej");
    w2.set_value(10);
    w3.set_value(20);
    std::cout << w1.get_value() << std::endl;
    std::cout << w2.get_value() << std::endl;
    std::cout << w3.get_value() << std::endl;

    w3 = w2;
    w1 = "men...";
    std::cout << w1.get_value() << std::endl;
    std::cout << w2.get_value() << std::endl;
    std::cout << w3.get_value() << std::endl;

    w1 = "jo..";
    w2 = 42;
    std::cout << w1;
    std::cout << w2;
    std::cout << w3;
    //




    return 0;
}

Thanks if you spent your time on this problem!

Upvotes: 0

Views: 97

Answers (1)

Ch3steR
Ch3steR

Reputation: 20669

There are few problems with your code.

  • Missing Constructor
template<typename T>
Wrapper<T>::Wrapper(T const& w): value(w){ }
  • Wrapper<T>::Wrapper is a constructor is just nothing meaningful. Wrapper<T>::Wrapper doesn't raise any error because of SFINAE
std::ostream& operator<<(std::ostream& os, Wrapper<T> const& w){ ... }
  • Return Wrapper<T>& instead of Wrapper<T>::Wrapper
Wrapper<T>& Wrapper<T>::operator=(T const& rhs) { ... }
  • std::cout is defined in iostream

Link to the demo

Checkout this answer on When is typename required

Upvotes: 2

Related Questions