纯虚拟功能和"cannot allocate an object"
Pure virtual function and "cannot allocate an object"
我有一个带有几个方法的基本抽象类。
base.h
class Base
{
..
..
virtual void example_1() = 0;
virtual void example_2() = 0;
};
example.h
class Example : public Base
{
virtual void example_1();
virtual void example_2();
};
example.c
void Example :: example_1()
{
cout << "1";
}
void Example :: example_2()
{
cout << "2";
}
纯虚函数必须在派生类中实现,我已经这样做了。但我仍然不清楚为什么我得到这个错误
错误"无法分配抽象类型" Example "的对象": Example .h 27:7:注意:因为以下虚函数在'Example'中是纯的:"
c: 225:25:
error: cannot allocate an object of abstract type 'Example'
In file included from main.c:328:0:
example.h:27:7: note: because the following virtual functions are pure within 'Example':
base.h:150:18: note: virtual void Base::example_1()
base.h:151:18: note: virtual void Base::example_2()
Edit:
在你添加了错误信息后,我在一个干净的c++项目上测试了你的代码,你的代码示例运行良好。
========== 构建:1成功,0失败了,最新的,0跳过 ==========
Base.h
#pragma once
class Base
{
private:
virtual void example_1() = 0;
virtual void example_2() = 0;
public:
Base();
~Base();
};
Base.cpp
#include "Base.h"
Base::Base()
{
}
Base::~Base()
{
}
Example.h
#pragma once
#include "Base.h"
class Example : public Base
{
public:
virtual void example_1();
virtual void example_2();
Example();
~Example();
};
Example.cpp
#include "Example.h"
#include <iostream>
using namespace std;
Example::Example()
{
}
void Example::example_1()
{
cout << "1";
}
void Example::example_2()
{
cout << "2";
}
Example::~Example()
{
}
Main.cpp
#include "Example.h"
int main()
{
Example ex;
ex.example_1();
return 0;
}
尝试将文件名从*.c
更改为*.cpp
在一个干净的项目中检查有问题的代码
你没有提供关于你的错误的完整信息,所以我猜这就是你正在处理的场景:
int main()
{
// Cannot instantiate, Base is abstract
Base base1;
// Should work
Example example;
// Should work
Base* base2 = new Example();
return 0;
}
在你的例子中,Base
是一个有两个纯虚函数的类-> Base
是一个抽象类。
因此你不能创建Base
的实例。
你可以创建一个Example
的实例,也可以有一个指向Base
的引用或指针。
相关文章:
- 模式"allocate memory or use existing data"
- N-API include an .so or dll
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 如何理解"abstract-declarator containing an ellipsis shall only be used in a parameter-declaration"
- 如何在数据库程序中添加"reduce the quantity of an item"功能?
- C++线程"Call to non-static member function without an object argument"
- Atom gpp编译器给出"'C:UsersadminUser' is not recognized as an internal or..."错误
- 尝试在类中编译内核,出现错误"__init__() got an unexpected keyword argument 'kernel'"
- 指向对象生存期之外的已分配内存的指针是"invalid pointer[s]"还是"pointer[s] to an object"?
- 如何修复" State Error (active) E0513 a value of type "const wchar_t *" cannot be assigned to an entity o
- 为什么'allocate in one library and free in the other'是错误的
- C++ libtins "An invalid handle was specified"
- "terminate called after throwing an instance of std::invalid_argument' what(): stoi ?"
- Hash for a std::pair, for use in an unordered_map
- Divide et impera sum of the elements of an array bug
- 为什么"an inherited constructor is not a candidate for initialization from an expression of the same or
- 根据标准,究竟什么是"declaration of an object"
- 抽象类和虚方法问题:"cannot allocate an object of abstract type"
- 为什么我会得到"cannot allocate an array of constant size 0"?
- 纯虚拟功能和"cannot allocate an object"