1

So, I just want to make this fucntion:

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

but I got this error on the line:

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

I wounder how can I cout the value of T.

Thanks in advance.

smac89
  • 32,960
  • 13
  • 112
  • 152
JT Woodson
  • 189
  • 1
  • 3

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
  • 2,724
  • 2
  • 22
  • 32