A C++ class-function
c++, class
Solution
You're trying to define a function that returns a T:
template<class T>
T TT::Read(const string& key) const
{
std::cout << key << std::endl;
return 1;
}
However, you always return an `int` from this function. You either need to call it like so:
tt.Read<int>("Hello");
Or remove the template definition, as it makes no sense here.
Problem
A simple c++ file and the class TT has two methods. ``` #include <map> #include <string> #include <iostream> using namespace std; class TT{ public: TT(const string& str); template<class T>T Read(const string& key)const; template<class T>T Read(const string& key, const T& value)const; }; TT::TT(const string& str){ cout<<str<<endl; } template<class T>T TT::Read(const string& key)const{ std::cout<<key<<std::endl; return 1; } template<class T>T TT::Read(const string& key, const T& value)const{ std::cout<<key<<'\t'<<value<<std::endl; return value; } int main(void){ TT tt("First"); tt.Read("Hello", 12); return 1; } ``` If replace the ``` tt.Read("Hello world!", 12); ``` with ``` tt.Read("Hello world!"); ``` in main() G++ says: new.cc:31: error: no matching function for call to ‘TT::Read(const char [5])’ Why G++ cann't find the Read(const string& key)const method? Thanks!