-2
\$\begingroup\$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.