# Why do I get this "no operator defined" error while making a logging system?

I am making a game engine and I'm working on a logging system. What I have so far is

 #include <iostream>

template <typename T>
void print(T n)
{
std::cout<<n<<std::endl;
}

template <typename T, typename... Args>
void print(T n, Args ... args)
{
std::cout<<n<<' '<<print(args...);
}

int main() {
print (5,4,3,2,1,'f');
}


And when I compile I get an error

 No operator<< defined with
std::basic_ostream<char> and void


When I change the void to std::basic_ostream I get

No operator<< defined with
std::basic_ostream<char> and std::basic_ostream<char>


Any ideas? Ideally I would like

Log (int logmode, ... itemsToPrint)


I'm using Ubuntu, c++11, and g++.

std::cout<<n<<' '<<print(args...);


You're trying to call print() and send the result of that call to std::cout via operator<<. This means the return value of print() needs to be something that std::ostream::operator<< can handle, and it can't handle void or std::ostream.

Given the context of your problem, however, you shouldn't need to call print() like that. You can just do:

std::cout<<n<<' ';
print(args...);

• Thank you. I figured since defining printing for a custom class returned an ostream I thought ostream << ostream would work. May 17 '17 at 22:09
• You mean defining a custom operator<<? That returns an ostream& so that you can "chain" the operators (foo << bar << baz).
– user1430
May 17 '17 at 22:11
• Operator << works left to right? Did not know that. Makes since though May 17 '17 at 22:12
• You might find en.cppreference.com/w/cpp/language/operators an interesting read.
– user1430
May 17 '17 at 22:15