有关虚函数的问题

myMugua 2011-08-02 10:43:49
public:
//创建主窗口
virtual CTest * CTest() { return new CTest2; }//这一句出了问题

//报错
error C2259: 'CGameClientDlg' : cannot instantiate abstract class
1> due to following members:
1> 'void CGameFrameDlg::OnEventContinueGame(void)' : is abstract
1> d:\vs2008\library\include\gameframedlg.h(120) : see declaration of 'CGameFrameDlg::OnEventContinueGame'

这个问题改怎么解决?
...全文
47 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
myMugua 2011-08-02
  • 打赏
  • 举报
回复
特别感谢dizuo! 每次都是你帮我解决的问题
luciferisnotsatan 2011-08-02
  • 打赏
  • 举报
回复
Error Message
'class' : cannot instantiate abstract class


Code declares an instance of an abstract class or structure.

You cannot instantiate a class or structure with one or more pure virtual functions. To instantiate objects of a derived class, the derived class must override each pure virtual function.

For more information, see Implicitly Abstract Classes.

The following sample generates C2259:

Copy Code
// C2259.cpp
// compile with: /c
class V {
public:
void virtual func() = 0;
};
class A : public V {};
class B : public V {
public:
void func();
};
V v; // C2259, V is an abstract class
A a; // C2259, A inherits func() as pure virtual
B b; // OK, B defines func()



Whenever you derive from an interface and implement the interface methods in the derived class with access permissions other than public, you may receive C2259. This occurs because the compiler expects the interface methods implemented in the derived class to have public access. When you implement the member functions for an interface with more restrictive access permissions, the compiler does not consider them to be implementations for the interface methods defined in the interface, which in turn makes the derived class an abstract class.

There are two possible workarounds for the problem:

Make the access permissions public for the implemented methods.

Use the scope resolution operator for the interface methods implemented in the derived class to qualify the implemented method name with the name of the interface.

C2259 can also occur as a result of conformance work that was done in Visual C++ 2005, /Zc:wchar_t is now on by default. In this situation, C2599 can be resolved either by compiling with /Zc:wchar_t-, to get the behavior from previous versions, or preferably, by updating your types so they are compatible. For more information, see Breaking Changes in the Visual C++ 2005 Compiler and /Zc:wchar_t (wchar_t Is Native Type).

The following sample generates C2259:

Copy Code
// C2259b.cpp
// compile with: /c
#include <windows.h>

class MyClass {
public:
// WCHAR now typedef'ed to wchar_t
virtual void func(WCHAR*) = 0;
};

class MyClass2 : MyClass {
public:
void func(unsigned short*);
};

MyClass2 x; // C2259

// OK
class MyClass3 {
public:
virtual void func(WCHAR*) = 0;
virtual void func2(wchar_t*) = 0;
virtual void func3(unsigned short*) = 0;
};

class MyClass4 : MyClass3 {
public:
void func(WCHAR*) {}
void func2(wchar_t*) {}
void func3(unsigned short*) {}
};

MyClass4 y;


The following sample generates C2259:

Copy Code
// C2259c.cpp
// compile with: /clr
interface class MyInterface {
void MyMethod();
};

ref class MyDerivedClass: public MyInterface {
private:
// Uncomment the following line to resolve.
// public:
void MyMethod(){}
// or the following line
// void MyInterface::MyMethod() {};
};

int main() {
MyDerivedClass^ instance = gcnew MyDerivedClass; // C2259
}


The following sample generates C2259:

Copy Code
// C2259d.cpp
// compile with: /clr:oldSyntax
public __gc __interface MyInterface {
void MyMethod();
};

__gc class MyDerivedClass : public MyInterface {
// Uncomment the following line to resolve.
// public:
void MyMethod() {};
// or the following line
// void MyInterface::MyMethod() {};
};

int main() {
MyDerivedClass *instance = new MyDerivedClass(); // C2259
}



myMugua 2011-08-02
  • 打赏
  • 举报
回复
public:
//创建主窗口
virtual CTest * CTest() { return new CTest2; }//这一句出了问题

//报错
error C2259: 'CGameClientDlg' : cannot instantiate abstract class
1> due to following members:
1> 'void CGameFrameDlg::OnEventContinueGame(void)' : is abstract
1> d:\vs2008\library\include\gameframedlg.h(120) : see declaration of 'CGameFrameDlg::OnEventContinueGame'
ryfdizuo 2011-08-02
  • 打赏
  • 举报
回复
看错误提示啊。。。
抽象类不能实例化,
CGameClientDlg中需要实现一下纯虚函数:OnEventContinueGame
bdmh 2011-08-02
  • 打赏
  • 举报
回复
哪个是abstract 类,abstract 类不能实例化

64,675

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧