从 c++ 中的类创建对象
Create an object from a class in c++
我刚开始用c ++编程,我不明白为什么Xcode说"变量类型'Rect'是一个抽象类"。你可以在这里找到我的一部分代码:提前感谢您的帮助,
main(){
Rect r1(10,5,"R1");
return 0;
}
class Rect : public Figure
{
public:
Rect(int l, int h,std::string Label) : Figure(l), _h(h),_Label(Label) {};
~Rect(){};
std:: vector<std::string> toString() const;
protected:
int _h;
int _l;
std::string _Label;
};
class Figure
{
public:
Figure(int l):_l(l){}
virtual std::vector<std::string> toStrings() const =0;
virtual ~Figure();
protected:
int _l;
};
这是标准错误。这意味着没有为要为其创建对象的类实现某些纯虚拟方法。
在这种情况下,Rect 必须实现 std::vector<std::string> toStrings() const
。要修复它:
class Rect : public Figure
{
public:
Rect(int l, int h,std::string Label) : Figure(l), _h(h),_Label(Label) {};
~Rect(){};
std:: vector<std::string> toStrings() const override
{
return {};
}
protected:
int _h;
int _l;
std::string _Label;
};
自C++11以来,override
关键字有助于发现此类错误。
相关文章:
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何创建对象函数指针C++映射?
- C++创建对象数组
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- 如何获取在 main() 函数中构造的类的创建对象?
- 基于文件中的条目创建对象
- 错误:创建对象后无法分配区域
- C++ 通过输入创建对象
- 堆还是堆栈用于创建对象?
- 使用 C++ 创建对象数组
- 使用unique_ptr创建对象
- C++递归地在类构造函数中创建对象
- 通过向构造函数其他对象引用页面来创建对象
- ReactiveX (rx) - 在对象上应用可观察对象,而不是在可观察对象中创建对象
- 如何在OSX上正确创建C++对象文件(.o)
- 编译器是否会创建vtable,而不考虑在c++中创建对象
- 创建对象并防止被破坏
- 在C++中,友元类可以从友元类创建对象吗
- 只在堆中创建C++对象
- 创建用户定义的复制构造函数时无法创建对象