C++ 错误:调用了纯虚拟方法
在我的AC.h文件中:C++ 错误:调用了纯虚拟方法,c++,C++,在我的AC.h文件中: #ifndef _AC_H #define _AC_H class AC{ private: unsigned short PC; public: AC():PC(0){} virtual ~AC(){} virtual void test()=0; }; #endif 在我的R500.h文件中: #ifndef _R500_H #define _R500_H #include <iostream> #include "AC.
#ifndef _AC_H
#define _AC_H
class AC{
private:
unsigned short PC;
public:
AC():PC(0){}
virtual ~AC(){}
virtual void test()=0;
};
#endif
在我的R500.h文件中:
#ifndef _R500_H
#define _R500_H
#include <iostream>
#include "AC.h"
class R500:public AC{
private: int mCStatus;
public:
R500():mCStatus(0){}
virtual ~R500(){}
void test();
};
#endif
在我的AF.cpp文件中:
#include <iostream>
#include "R500.h"
#include "AF.h"
#include "AC.h"
using namespace std;
void AF::menu() {
R500 r;
ac = &r;
run();
}
void AF::run() {
ac->test();
}
#包括
#包括“R500.h”
#包括“AF.h”
#包括“AC.h”
使用名称空间std;
void AF::menu(){
R500 r;
ac=&r;
run();
}
void AF::run(){
ac->test();
}
在我的Main.cpp中
#include <iostream>
#include "AF.h"
#include "R500.h"
#include "AC.h"
using namespace std;
int main(int args, char**argv){
AF af;
af.menu();
return 0;
}
#包括
#包括“AF.h”
#包括“R500.h”
#包括“AC.h”
使用名称空间std;
int main(int参数,字符**argv){
房颤;
af.菜单();
返回0;
}
它编译得很好,但当我运行时,它说名为
的纯虚拟方法 在没有活动异常的情况下终止调用
中止
谁能告诉我哪里错了? 谢谢。
在代码的某个地方(显然您并没有发布),您从构造函数或析构函数调用了虚函数。这个函数在您调用它的构造函数/析构函数所在的类中恰好是纯虚拟的
您得到了纯虚拟调用
,因为此时,完整的子对象要么尚未完全构建,要么已经销毁。并用全子对象销毁其虚函数表。所以你们只剩下抽象类中的虚函数表,通过这个表你们调用了纯虚函数
也请检查此链接:
在代码的某个地方(显然您并没有发布),您从构造函数或析构函数调用了虚函数。这个函数在您调用它的构造函数/析构函数所在的类中恰好是纯虚拟的
您得到了纯虚拟调用
,因为此时,完整的子对象要么尚未完全构建,要么已经销毁。并用全子对象销毁其虚函数表。所以你们只剩下抽象类中的虚函数表,通过这个表你们调用了纯虚函数
同时检查此链接:您有两个
AC*AC的实例代码>
一个在AF.h中定义的AF类中,另一个在AF.cpp中具有全局范围。
您发布的代码并不完全是导致问题的代码,因此很难告诉您更多信息。您有两个AC*AC代码>
一个在AF.h中定义的AF类中,另一个在AF.cpp中具有全局范围。
您发布的代码并不完全是导致问题的代码,因此很难告诉您更多信息。您的代码中有几点可以改进
但对于您的问题,此代码在Visual Studio 2008上编译并运行良好:
#include <iostream>
using namespace std;
class AC
{
private:
unsigned short PC;
public:
AC():PC(0) {}
virtual ~AC() {}
virtual void test() = 0;
};
class R500 : public AC
{
private:
int mCStatus;
public:
R500(): mCStatus(0) {}
virtual ~R500() {}
void test();
};
void R500::test()
{
cout << "test called\n";
}
class AF
{
public:
int status;
AC *ac;
AF() : status(1) {} // You forgot to add a body to your constructor here "{}"
void menu();
void run();
};
void AF::menu()
{
R500 r;
ac = &r;
run();
}
void AF::run()
{
ac->test();
}
int main(int args, char** argv)
{
AF af;
af.menu();
return 0;
}
#包括
使用名称空间std;
AC类
{
私人:
无符号短PC;
公众:
AC():PC(0){}
虚拟~AC(){}
虚空测试()=0;
};
R500类:公共空调
{
私人:
国际地位;
公众:
R500():mCStatus(0){}
虚拟~R500(){}
无效试验();
};
void R500::test()
{
cout测试();
}
int main(int参数,字符**argv)
{
房颤;
af.菜单();
返回0;
}
我只做了两件事:
- 将主体添加到AF的构造函数中
- 移除全局变量ac
您的代码中有几个地方可以改进
但对于您的问题,此代码在Visual Studio 2008上编译并运行良好:
#include <iostream>
using namespace std;
class AC
{
private:
unsigned short PC;
public:
AC():PC(0) {}
virtual ~AC() {}
virtual void test() = 0;
};
class R500 : public AC
{
private:
int mCStatus;
public:
R500(): mCStatus(0) {}
virtual ~R500() {}
void test();
};
void R500::test()
{
cout << "test called\n";
}
class AF
{
public:
int status;
AC *ac;
AF() : status(1) {} // You forgot to add a body to your constructor here "{}"
void menu();
void run();
};
void AF::menu()
{
R500 r;
ac = &r;
run();
}
void AF::run()
{
ac->test();
}
int main(int args, char** argv)
{
AF af;
af.menu();
return 0;
}
#包括
使用名称空间std;
AC类
{
私人:
无符号短PC;
公众:
AC():PC(0){}
虚拟~AC(){}
虚空测试()=0;
};
R500类:公共空调
{
私人:
国际地位;
公众:
R500():mCStatus(0){}
虚拟~R500(){}
无效试验();
};
void R500::test()
{
cout测试();
}
int main(int参数,字符**argv)
{
房颤;
af.菜单();
返回0;
}
我只做了两件事:
- 将主体添加到AF的构造函数中
- 移除全局变量ac
背景:在构造C++对象时,首先构造基类,然后依次依次使用中间类完成每个中间类。在构造每个层时,它只能访问自己或超类定义的虚拟函数。当物体被破坏时,同样的机制也会发生,尽管是相反的
如果构造函数/析构函数调用一个只由子类定义的虚函数,则会触发纯虚函数称为错误,这将立即中止程序。
背景:在构造C++对象时,首先构造基类,然后依次依次初始化每个中间类,完成混凝土类。在构造每个层时,它只能访问自己或超类定义的虚拟函数。当物体被破坏时,同样的机制也会发生,尽管是相反的
如果a构造函数/析构函数调用一个仅由子类定义的虚拟函数,您将触发名为error的纯虚拟函数,该函数将立即中止您的程序。我肯定几天前我用不同的非实代码看到了这个问题,但我现在找不到。键入错误,抱歉。。。我已经有了AF():status(1){}//这是一个构造函数,找到了可能的dup我是肯定的,我几天前看到了这个问题,使用了不同的非真实代码,但我现在找不到它。键入错误,抱歉。。。我已经有了AF():status(1){}//这是一个可能的dup或他正在切片的构造函数。或者混合了全局/成员/局部变量。我不相信编译器会将切片传递给抽象类的子对象。或者他正在切片。或者混合了全局/成员/局部变量。我不相信编译器会将切片传递给抽象类的子对象。这是真实代码,AF.cpp中的ac变量,这是一个键入错误,对不起,这是真实代码,AF.cpp中的ac变量,这是一个键入错误,对不起
#include <iostream>
using namespace std;
class AC
{
private:
unsigned short PC;
public:
AC():PC(0) {}
virtual ~AC() {}
virtual void test() = 0;
};
class R500 : public AC
{
private:
int mCStatus;
public:
R500(): mCStatus(0) {}
virtual ~R500() {}
void test();
};
void R500::test()
{
cout << "test called\n";
}
class AF
{
public:
int status;
AC *ac;
AF() : status(1) {} // You forgot to add a body to your constructor here "{}"
void menu();
void run();
};
void AF::menu()
{
R500 r;
ac = &r;
run();
}
void AF::run()
{
ac->test();
}
int main(int args, char** argv)
{
AF af;
af.menu();
return 0;
}