【发布时间】:2013-10-07 06:54:41
【问题描述】:
调用TestComp的构造函数时出现编译错误,其设计如下:
template <typename R>
class IComparable
{
public:
virtual bool Equals(const R & rhs) const = 0;
};
class TestComp : IComparable<char*>
{
public:
std::string x;
TestComp(std::string & a)
{
x = a;
}
virtual bool Equals(const char* & a) const
{
return x == std::string(a);
}
};
错误:
error C2259: 'TestComp2' : cannot instantiate abstract class due to following members:'bool IComparable<R>::Equals(const R &) const' : is abstract with [ R=char * ]
我无法理解,因为我正在定义 TestComp::Equals,其签名似乎与 IComparable::Equals 相同。
我在尝试各种解决方法时注意到的一件事是,如果我将两个函数都设为“Equals(R & rhs) const”,从参数中删除 const,那么我将不再收到此错误。
谁能帮我理解这种行为?
【问题讨论】:
标签: c++ templates inheritance pure-virtual