1

So, I just want to make this function:

template<typename T>
void printWithEndl(T)
{
    std::cout << T << "\n";
}

but I got this error on the line:

std::cout << T << "\n";

I wonder: how can I cout the value of T?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JT Woodson
  • 249
  • 2
  • 5

1 Answers1

10

You should name the variable you're passing to printWithEndl, and cout that name:

template<typename T>
void printWithEndl(T msg)
{
    std::cout << msg << "\n";
}

If you're using this to print complex objects, you're probably better off passing a reference to const:

template<typename T>
void printWithEndl(const T& msg)
{
    std::cout << msg << "\n";
}
Jack Deeth
  • 3,062
  • 3
  • 24
  • 39