并感谢您的帮助。这是我第一次发帖!
我正在尝试将 Square 类构造函数传递给 Rectangle 类构造函数,但出现以下错误:
Square.cpp:9: 错误: 重新定义 'Square::Square(double)' Square.hpp:19: 错误: 'Square::Square(double)' 之前在这里定义
目标是将 Square 类中的单个参数(我猜是两次)传递给父 Rectangle 类(它接受 2 个参数)。
这是我的代码:
// Square.hpp
#ifndef SQUARE_HPP
#define SQUARE_HPP
#include "Rectangle.hpp"
using namespace std;
class Square : public Rectangle
{
//private:
public:
Square(double sideSquare):Rectangle(sideSquare, sideSquare){};
//Square(double sideSquare):Rectangle(double, double){}
void setLength(double);
void setWidth(double);
};
#endif
// Square.cpp
#include "Square.hpp"
using namespace std;
Square::Square(double sideSquare) : Rectangle (sideSquare, sideSquare){}
void Rectangle::setLength(double lengthIn)
{length = lengthIn; width = lengthIn;}
void Rectangle::setWidth(double widthIn)
{width = widthIn; length = widthIn;}
// squareMain.cpp
#include "Square.hpp"
#include <iostream>
using namespace std;
int main ()
{
Rectangle box1(14, 11.5);
cout << "box1 area: " << box1.area() << endl;
cout << "box1 perimeter: " << box1.perimeter() << endl;
return 0;
}
我没有包括 Rectangle.hpp 和 Rectangle.cpp。非常感谢您的帮助,在此先感谢