I am facing the following problem: I am trying to create a function to print a generic pointer information on the screen. This is the code:
#include <iostream>
#include <string>
template <class T_str, class T>
std::basic_string<T_str> ptr( T* ptr )
{
std::basic_ostringstream<T_str> oss;
oss << "Value: " << ptr << "\n";
oss << "Direction: " << &ptr << "\n";
return oss.str();
}
int main()
{
int *p;
std::cout << ptr( p );
}
But I got the following error:
other.cpp: In function ‘int main()’:
other.cpp:15:19: error: no matching function for call to ‘ptr(int*&)’
15 | std::cout << ptr( p );
| ~~~^~~~~
other.cpp:4:26: note: candidate: ‘template<class T_str, class T> std::__cxx11::basic_string<_CharT> ptr(T*)’
4 | std::basic_string<T_str> ptr( T* ptr )
| ^~~
other.cpp:4:26: note: template argument deduction/substitution failed:
other.cpp:15:19: note: couldn’t deduce template parameter ‘T_str’
15 | std::cout << ptr( p );
I don't understand what is the problem. Can you help me? Thanks.