0

I have a class which I'm trying to stream directly into (I'm not streaming the class into an ostream, but actually streaming text into the class) by overriding the << operator. The class is essentially used so we can toggle debug output on and off for different parts of the project. However, when I try to use the operator, I get an undefined reference.

This is what's in my .h as a public member of the class

template <typename T>
DebugOutputStreamer& operator<<(T const& value);

And this is in my .cpp

template <typename T>
DebugOutputStreamer& DebugOutputStreamer::operator <<(T const& value)
{
    if(m_Enabled)
    {
        if(m_UseColors)
        {
            std::cout << m_OuputColour << value << ConsoleColour::FG_DEFAULT;
        }
        else
        {
            std::cout << value;
        }
    }
    return this;
}

However when I try to stream "Hello" in, I'm getting

main.cpp:(.text+0x564): undefined reference to `DebugOutputStreamer& DebugOutputStreamer::operator<< <char [6]>(char const (&) [6])'

Could anybody tell me what I'm missing?

Sam
  • 374
  • 1
  • 3
  • 14
  • 1
    Put the template definition in the .h – tux3 Apr 09 '15 at 09:02
  • OK I moved the code out of the .cpp, and now I get error: invalid initialization of non-const reference of type 'DebougOutputStreamer&' from a temporary of type 'DebugOutputStreamer* const' when I try to return this. – Sam Apr 09 '15 at 09:08
  • Of course, thanks. Still gettting some errors when I try to stream more than one thing (i.e. << "Hello" << std::endl;) but I think that's more to do with how I'm returning, so it's not relevant to the original question. Also apologies for the duplicate, didn't realise it was to do with the templating, so didn't know what to search correctly. – Sam Apr 09 '15 at 09:34

0 Answers0