I am trying to compile the following code in VC ++ 2010:
class Base { public: std::wstring GetString(unsigned id) const { return L"base"; } }; class Derived : public Base { public: std::wstring GetString(const std::wstring& id) const { return L"derived"; } }; int wmain(int argc, wchar_t* argv[]) { Derived d; d.GetString(1); }
I realized that Derived will have two methods:
std::wstring GetString(unsigned id) const std::wstring GetString(const std::wstring& id) const
so my code must compile successfully. Visual C ++ 2010 reports the following error:
test.cpp(32): error C2664: 'Derived::GetString' : cannot convert parameter 1 from 'int' to 'const std::wstring &' Reason: cannot convert from 'int' to 'const std::wstring' No constructor could take the source type, or constructor overload resolution was ambiguous
What am I doing wrong? The code works fine when I use it like this:
Derived d; Base base = d; base.GetString(1);
or when both GetString variants are defined in the same class.
Any ideas? I would like to avoid explicit typecasting.
imagi source share