形状头文件
错误:“矩形”的构造函数必须显式初始化没有默认构造函数的基类“形状”
#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 */形状cpp文件
错误:重新定义“矩形”
#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;
}基类头文件
#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 */基类cpp文件
#include "shape.hpp"
Shape::Shape(const char* name){
m_name = name;
}
char Shape::getType(){
return *m_name ;
}我做了另一个类“圆圈”,布局与矩形相同,没有任何错误,这些错误只出现在矩形类上。我被困住了,不知道为什么。
发布于 2015-12-14 04:29:14
在头文件中,使用空体Rectangle定义{}构造函数。
在CPP文件中,您再次定义了Rectangle构造函数。它在抱怨重复。
您的头文件应该只包含声明:
Rectangle(double length, double width);发布于 2015-12-14 04:33:43
这个错误不应该存在,在我的机器上也不存在(GCC 4.9.2)
在您的Rectangle.hpp中,您必须定义类矩形的构造函数,只需声明它
Rectangle(double length,double width)https://stackoverflow.com/questions/34259752
复制相似问题