C++构造函数继承错误
C++ constructor inheritance error
为什么以下代码是
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
Polygon()
{
cout<<"Constructor with no argumentsn";
width = 0;
height = 0;
}
Polygon(int width,int height)
{
cout<<"Constructor with 2 argumentsn";
this->width = width;
this->height = height;
}
};
class Rectangle: public Polygon {
public:
Rectangle(int width,int height):Polygon(width,height){}
int area ()
{ return width * height; }
};
class Triangle: public Polygon {
public:
Trianlge(int width,int height): Polygon(width,height){}
int area ()
{ return width * height / 2; }
};
int main () {
//Rectangle rect(4,4);
//Triangle trgl(4,4);
return 0;
}
导致这些错误:
test.cpp:34:39: error: ISO C++ forbids declaration of ‘Trianlge’ with no type [-fpermissive]
Trianlge(int width,int height): Polygon(width,height){}
^
test.cpp: In member function ‘int Triangle::Trianlge(int, int)’:
test.cpp:34:42: error: only constructors take member initializers
Trianlge(int width,int height): Polygon(width,height){}
^
test.cpp:34:64: warning: no return statement in function returning non-void [-Wreturn-type]
Trianlge(int width,int height): Polygon(width,height){}
这是构造函数的继承问题。每次创建矩形或三角形时,我都想调用Polygon的构造函数。然而,让我震惊的是,类Rectangle
和Triangle
非常相似,并且我只在Triangle
中出错,而在Rectangle
中没有。你能给我解释一下出错的原因吗?我该怎么修?
您的Triangle
类中有一个打字错误
Trianlge(int width,int height): Polygon(width,height){}
^
^
相关文章:
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 在 c++ 中拆分类和标头中的继承,错误
- enable_if继承的成员函数的名称查找错误
- C++模板类 - 继承调用错误的函数
- C++ 中的构造函数、继承、堆栈、堆、this-pointer 和段错误
- 为什么这个混合继承程序给出错误的输出?
- 使用多重继承时出现编译错误
- 多级继承错误 C2280:"atttempting to reference a deleted function"
- 相对于继承的构造函数,gcc 编译器是否还有一个错误?
- 从模板继承的 MSVC DLL 导出类会导致LNK2005已定义的错误
- 模板化类继承的子类定义中的错误
- C++错误LNK2005和不同项目文件的常规继承
- 错误:'int_type'未命名类型 - 如何继承 typedefs 和用法
- 在 soap_ssl_server_context(..) 中使用继承时 gsoap 中的分段错误
- C++中的继承,"...is an ambiguous base of ..."错误
- 与lambda一起使用虚拟继承在初始化列表中捕获此问题的GCC错误
- 收到未定义的引用错误 c++ 继承/多态性
- c++错误:继承转换无效
- 错误继承不能访问c++的方法