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?
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?
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";
}