C++;实现扩展子接口而不重写已重写的方法的子类 我创建了接口(抽象类),在C++中扩展了其他接口,我试图实现它们,但是在编译时发生了错误。
以下是错误:C++;实现扩展子接口而不重写已重写的方法的子类 我创建了接口(抽象类),在C++中扩展了其他接口,我试图实现它们,但是在编译时发生了错误。,c++,inheritance,abstract-class,C++,Inheritance,Abstract Class,以下是错误: main.cpp: In function 'int main()': main.cpp:36:38: error: cannot allocate an object of abstract type 'Subclass' Subclass * subObj = new Subclass(); ^ Subclass.h:13:7: note: because the following vi
main.cpp: In function 'int main()':
main.cpp:36:38: error: cannot allocate an object of abstract type 'Subclass'
Subclass * subObj = new Subclass();
^
Subclass.h:13:7: note: because the following virtual functions are pure within 'Subclass':
class Subclass : SubInterface {
^
SuperInterface.h:13:18: note: virtual void SuperInterface::doSomething()
virtual void doSomething()=0;
#include <iostream>
class SuperInterface {
public:
virtual void doSomething() = 0;
protected:
int someValue;
};
class SubInterface : public SuperInterface {
public:
virtual void doSomethingElseThatHasNothingToDoWithTheOtherMethod() = 0;
protected:
int anotherValue;
};
class Superclass : public SuperInterface {
public:
Superclass() {}
virtual ~Superclass() {}
void doSomething() {std::cout << "hello stackoverflow!";}
};
class Subclass : public SubInterface {
public:
Subclass() {}
virtual ~Subclass() {}
void doSomethingElseThatHasNothingToDoWithTheOtherMethod() {std::cout << "goodbye stackoverflow!";}
};
int main(void)
{
Superclass * superObj = new Superclass();
Subclass * subObj = new Subclass();
}
以下是我的资料来源:
main.cpp: In function 'int main()':
main.cpp:36:38: error: cannot allocate an object of abstract type 'Subclass'
Subclass * subObj = new Subclass();
^
Subclass.h:13:7: note: because the following virtual functions are pure within 'Subclass':
class Subclass : SubInterface {
^
SuperInterface.h:13:18: note: virtual void SuperInterface::doSomething()
virtual void doSomething()=0;
#include <iostream>
class SuperInterface {
public:
virtual void doSomething() = 0;
protected:
int someValue;
};
class SubInterface : public SuperInterface {
public:
virtual void doSomethingElseThatHasNothingToDoWithTheOtherMethod() = 0;
protected:
int anotherValue;
};
class Superclass : public SuperInterface {
public:
Superclass() {}
virtual ~Superclass() {}
void doSomething() {std::cout << "hello stackoverflow!";}
};
class Subclass : public SubInterface {
public:
Subclass() {}
virtual ~Subclass() {}
void doSomethingElseThatHasNothingToDoWithTheOtherMethod() {std::cout << "goodbye stackoverflow!";}
};
int main(void)
{
Superclass * superObj = new Superclass();
Subclass * subObj = new Subclass();
}
#包括
类超级接口{
公众:
虚空doSomething()=0;
受保护的:
int值;
};
类子接口:公共超级接口{
公众:
与TheTheTheThetherMethod()无关的虚拟无效剂量Thingelset=0;
受保护的:
内在价值;
};
类超类:公共超级接口{
公众:
超类(){}
虚~Superclass(){}
void doSomething(){std::cout出现错误是因为您试图创建抽象类的对象。
您的子类是一个抽象类,因为这一行void doSomethingElse()=0;
。
如果一个类有一个纯虚函数,它将是一个抽象类。你不能创建一个抽象类的对象,你只能有一个指向它的引用或指针
为了消除错误,应该在子类
中声明doSomethingElse
void doSomethingElse();
而不是void doSomethingElse()=0;
另外,我不明白为什么需要两个接口。您可以从超级接口
派生子类
,因为它基本上与子接口
相同,因为您试图创建抽象类的对象,所以会出现错误。
您的子类是一个抽象类,因为这一行void doSomethingElse()=0;
。
如果一个类有一个纯虚函数,它将是一个抽象类。你不能创建一个抽象类的对象,你只能有一个指向它的引用或指针
为了消除错误,应该在子类
中声明doSomethingElse
void doSomethingElse();
而不是void doSomethingElse()=0;
另外,我不明白为什么需要两个接口。您可以从超级接口
派生子类
,因为它基本上与子接口
相同。您的类“子类”应该覆盖两个纯虚拟方法,因此:
class Subclass : SubInterface {
public:
Subclass();
virtual ~Subclass();
void doSomethingElse() override;
void doSomething() override;
};
不这样做或声明
void doSomethingElse()=0
类子类也变得抽象,无法实例化。您可以看看:
以下是我想要的:我希望我的实现能够被感知,因此
与已重写方法的行为相同(例如
subObj->doSomething()方法无需实现即可工作
有没有人能告诉我,如果
这甚至是可能的!!??谢谢
-->可能将方法声明为virtual而非pure virtual您的类“Subclass”应重写2个pure virtual方法,因此:
class Subclass : SubInterface {
public:
Subclass();
virtual ~Subclass();
void doSomethingElse() override;
void doSomething() override;
};
不这样做或声明
void doSomethingElse()=0
类子类也变得抽象,无法实例化。您可以看看:
以下是我想要的:我希望我的实现能够被感知,因此
与已重写方法的行为相同(例如
subObj->doSomething()方法无需实现即可工作
有没有人能告诉我,如果
这甚至是可能的!!??谢谢
-->可能会声明方法是虚拟的而不是纯虚拟的老实说,我不完全确定您的设计想要表达什么,但这里至少有两个技术错误:
1.)您在所有情况下都使用私有继承,因此实际上根本不处理“接口”。公共继承是这样实现的:
class SubInterface : public SuperInterface
2.)对于显然想要实现的函数,使用=0
这将修复编译器错误,但设计仍有疑问。考虑到您在问题末尾给出的动机,我建议使用组合而不是(公开)继承。在C++中,共享功能最好用构词来表达。为了简单地说,将常用的功能封装在一个单独的类中,并将其他类与它的一个对象进行组合。
class CommonFunctionality
{
//...
public:
void doSomething();
void doSomethingElse();
};
class SuperClass
{
//...
private:
CommonFunctionality m_functionality;
};
class SubClass : public SuperClass
{
//...
private:
CommonFunctionality m_functionality;
};
事实上,也许您甚至不需要为CommonFunction创建类。也许简单的独立函数就可以了。有Java背景的程序员(您的代码看起来有点像它)在C++中必须将太多的东西放在课堂上。 老实说,我不完全清楚你的设计想要表达什么,但是这里至少有两个技术错误:
1.)您在所有情况下都使用私有继承,因此实际上根本不处理“接口”。公共继承是这样实现的:
class SubInterface : public SuperInterface
2.)对于显然想要实现的函数,使用=0
这将修复编译器错误,但设计仍有疑问。考虑到您在问题末尾给出的动机,我建议使用组合而不是(公开)继承。在C++中,共享功能最好用构词来表达。为了简单地说,将常用的功能封装在一个单独的类中,并将其他类与它的一个对象进行组合。
class CommonFunctionality
{
//...
public:
void doSomething();
void doSomethingElse();
};
class SuperClass
{
//...
private:
CommonFunctionality m_functionality;
};
class SubClass : public SuperClass
{
//...
private:
CommonFunctionality m_functionality;
};
事实上,也许您甚至不需要为CommonFunction创建类。也许简单的独立函数就可以了。有Java背景的程序员(您的代码看起来有点像它)在C++中必须把太多的东西放进类中。 有2个问题,编译器清楚地说明了:
问题1
子类
中的子接口::doSomethingElse()()
被声明为纯虚拟,忽略您试图在源文件中定义它(我很确定,这是一种复制粘贴类错误)
其他问题:
main.cpp: In function 'int main()':
main.cpp:36:38: error: cannot allocate an object of abstract type 'Subclass'
Subclass * subObj = new Subclass();
^
Subclass.h:13:7: note: because the following virtual functions are pure within 'Subclass':
class Subclass : SubInterface {
^
SuperInterface.h:13:18: note: virtual void SuperInterface::doSomething()
virtual void doSomething()=0;
#include <iostream>
class SuperInterface {
public:
virtual void doSomething() = 0;
protected:
int someValue;
};
class SubInterface : public SuperInterface {
public:
virtual void doSomethingElseThatHasNothingToDoWithTheOtherMethod() = 0;
protected:
int anotherValue;
};
class Superclass : public SuperInterface {
public:
Superclass() {}
virtual ~Superclass() {}
void doSomething() {std::cout << "hello stackoverflow!";}
};
class Subclass : public SubInterface {
public:
Subclass() {}
virtual ~Subclass() {}
void doSomethingElseThatHasNothingToDoWithTheOtherMethod() {std::cout << "goodbye stackoverflow!";}
};
int main(void)
{
Superclass * superObj = new Superclass();
Subclass * subObj = new Subclass();
}
- 你是天生的