当问题在性质上完全不同时,为什么要使用 C2259?
Why a C2259 when the problem is utterly different in nature?
尝试编译下面的代码片段时,我得到了错误:
error C2259: 'cTest': cannot instantiate abstract class
note: due to the following members:
note: void iTest::log(iTest::eLevel): is abstract
note: see declaration of 'iTest::log'
抽象成员函数已使用相同的签名定义。
删除第二个枚举可以解决问题,无论如何它都不是故意的。
但是我仍然不知道为什么给出 C2259,我只能发现应该定义成员来修复。
class iTest
{
public:
enum eLevel
{
Info,
};
virtual void foo( eLevel l ) = 0;
};
class cTest : public iTest
{
public:
enum eLevel
{
Info,
};
virtual void foo( eLevel l )
{
}
};
int main()
{
iTest* t = new cTest();
}
您收到此错误的原因是iTest::foo
和cTest::foo
的第二个定义没有相同的签名,因此ctest
没有实现iTest::foo
。
使用全名,您的声明如下:
class iTest
{
public:
enum eLevel
{
Info,
};
virtual void foo( iTest::eLevel l ) = 0;
};
class cTest : public iTest
{
public:
enum eLevel
{
Info,
};
virtual void foo( cTest::eLevel l )
{
}
};
可以看出,foo(iTest::eLevel)
与foo(cTest::eLevel)
不同,因此cTest
仍然是一个抽象类,不能实例化。
您可以完全删除cTest::eLevel
,也可以cTest::foo
声明为
class cTest : public iTest
{
public:
enum eLevel
{
Info,
};
virtual void foo( iTest::eLevel l )
{
}
};
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么在全局范围内使用"extern int a"似乎不行?
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 为什么会发生堆损坏
- 为什么使用 "this" 指针调用派生成员函数?
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么比较运算符如此快速
- 为什么 Serial.println(<char[]>);返回随机字符?
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 为什么不;名字在地图上是按顺序排列的吗
- 我的字符计数代码计算错误.为什么
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 为什么 std::unique 不调用 std::sort?
- 既然存在危险,为什么项目要使用-I include开关
- 当问题在性质上完全不同时,为什么要使用 C2259?