[英]c++ inheritance /redefinition error
The Shape Header File 形状标题文件
ERROR: Constructor for 'Rectangle' must explicitly initialize the base class 'Shape' which does not have a default constructor 错误:“矩形”的构造函数必须显式初始化没有默认构造函数的基类“ Shape”
#ifndef Rectangle_hpp
#define Rectangle_hpp
#include "shape.hpp"
#include <stdio.h>
class Rectangle:public Shape{
double m_length;
double m_width;
public:
Rectangle(double length,double width):Shape("Rectangle"){}
double getPerimeter();
double getArea();
};
#endif /* Rectangle_hpp */
The Shape cpp File 形状cpp文件
ERROR: Redefinition of 'Rectangle' 错误:“矩形”的重新定义
#include "Rectangle.hpp"
#include "shape.hpp"
#include "Rectangle.hpp"
Rectangle::Rectangle(double length,double width):Shape("Rectangle"){
m_length = length;
m_width = width;
}
double Shape::getPerimeter(){
return 2;
}
double Shape::getArea(){
return 2;
}
BASE class Header File BASE类头文件
#ifndef shape_hpp
#define shape_hpp
#include <stdio.h>
class Shape{
const char* m_name;
public:
Shape(const char* name);
virtual double getPerimeter()=0;
virtual double getArea()=0;
char getType();
};
#endif /* shape_hpp */
Base Class cpp file 基类cpp文件
#include "shape.hpp"
Shape::Shape(const char* name){
m_name = name;
}
char Shape::getType(){
return *m_name ;
}
I made another class "Circle" with the same layout as Rectangle and didnt get any errors, these errors are only appearing the the rectangle class.. i am stuck and have no idea why. 我用与Rectangle相同的布局制作了另一个类“ Circle”,但未收到任何错误,这些错误仅出现在矩形类中。.我被卡住了,不知道为什么。
In your header file you define the Rectangle
constructor with an empty body {}
. 在头文件中,使用空主体
{}
定义Rectangle
构造函数。
In you CPP file you define the Rectangle
constructor again. 在CPP文件中,再次定义
Rectangle
构造函数。 It's complaining about the duplication. 它在抱怨重复。
Your header file should only contain the declaration: 您的头文件应仅包含声明:
Rectangle(double length, double width);
this error should not exist and it does not in my machine (GCC 4.9.2) 此错误应该不存在,并且不在我的计算机中(GCC 4.9.2)
In your Rectangle.hpp, you had define the constructor of class Rectangle, you need just to declare it 在Rectangle.hpp中,您已经定义了Rectangle类的构造函数,只需声明它
Rectangle(double length,double width)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.