xcode 为什么我的派生类是抽象类?C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26584870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why my derived class is an abstract class? C++
提问by user4185295
//abstract product
class AbstractProduct
{
public:
virtual void diplay(void) = 0;
};
//concrete product
class Histogram : public AbstractProduct
{
public:
Histogram()
{
cout << "default construct a Histogram!" << endl;
}
void display(void)
{
cout << "Display Histogram!" << endl;
}
};
//factury class
class Factury
{
public:
static AbstractProduct* getProduct(string type)
{
AbstractProduct* absP;
if(type=="Histogram")
{
Histogram his;// error, Variable type "Histogram" is an abstract class
}
return absP;
}
};
I have already implement the pure virtual function, but it still said my derived class is an abstract class. I do not know why.
我已经实现了纯虚函数,但是还是说我的派生类是抽象类。我不知道为什么。
回答by Vlad from Moscow
The problem is that these functions
问题是这些函数
virtual void diplay(void) = 0;
and
和
void display(void)
{
cout << "Display Histogram!" << endl;
}
have different names. I think there is simply a typo.
有不同的名字。我认为这只是一个错字。
Take into account that this static function
考虑到这个静态函数
static AbstractProduct* getProduct(string type)
{
AbstractProduct* absP;
if(type=="Histogram")
{
Histogram his;// error, Variable type "Histogram" is an abstract class
}
return absP;
}
also have no sense because it returns uninitialized pointer absP
也没有意义,因为它返回未初始化的指针 absP
回答by djna
If that really is your code you have a typo
如果那真的是你的代码,你有一个错字
virtual void diplay(void) = 0;
and
和
void display(void)