Does anyone knows why this code cannot be compiled in visual studio 2013? The problem lies in this that b.a() has only one version (the overrided one in B class a(float)) and version a(std::string) is unavailable although it is in base class.
#include <string>
template <typename T>
class A {
public:
virtual void a(std::string b){ this->a(123); }
virtual void a(float b) = 0;
};
class B : public A < std::string > {
public:
virtual void a(float b) override {}
};
main()
{
B b;
b.a(""); // Error here: error C2664:
// 'void B::a(float)' : cannot convert argument 1 from 'const char [1]' to 'float'
B* bb = new B();
bb->a(""); // same
}
If a derived class declares a name and you also want members of this name from base classes to be visible, you need to unhide those names explicitly, using using
:
class B : public A<std::string>
{
public:
using A<std::string>::a;
virtual void a(float b) override {}
};
Now you can use all overloads of a
:
B x;
x.a(1.2);
x.a("hello");
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments