gpt4 book ai didi

c++ - 预期的构造函数、析构函数或 header

转载 作者:行者123 更新时间:2023-11-30 03:48:02 25 4
gpt4 key购买 nike

该程序包含三个部分:标题、类的函数和交互的主要部分。但是,它不会编译。

我不断收到响应,指出存在预期的构造函数、析构函数或类型转换。

#ifndef BOX_H
#define BOX_H

class Box
{

private:
double height;
double width;
double length;

public:
Box();
double setHeight();
double setWidth();
double setLength();
double getVolume();
double getSurfaceArea();
};
#endif

函数.cpp:

#include "Box.hpp"

/**********************************************************************
Box:: Box
This is the default constructor that uses the set methods to initialize each field to 1.
* **********************************************************************/
Box::Box()
{
height = 1;
width = 1;
length = 1;
}
/*
Does anyone know what this section does? Is it another default constructor or is is it even needed?
Box::Box(double height, double width, double length)
{
setHeight(height);
setWidth(width);
setLength(length);
}

*/
double Box::setHeight()
{
return height;
}

double Box::setWidth()
{
return width;
}

double Box::setLength()
{
return length;
}

double Box::getVolume()
{
return height * width * length;
}

double Box::getSurfaceArea()
{
double SurAre = 0;
SurAre = (2 * (length * width)) + (2 * (length * height)) + (2 * (height * width));
return SurAre;
}

main.cpp:

#include <iostream>
#include "Box.hpp" //contains Box class declaration
using namespace std;

int main()
{
Box object;

double Alength;
double Awidth;
double Aheight;

cout << "This program will calculate the area of a box.\n";
cout << "What is the length?";
cin >> Alength;
cout << "What is the width?";
cin >> Awidth;
cout << "What is the height?";
cin >> Aheight;

object.setLength(Alength);
if (!object.setWidth(Awidth))
cout << "Invalid box width entered. \n" << endl;
if (!object.setHeight(Aheight))
cout << "Invalid box height entered. \n" << endl;



cout << "Volume: " << object.getVolume() << endl;
cout << "Surface Area: " << object.getSurfaceArea() << endl;
return 0;

}

有人知道为什么吗?

最佳答案

如果你取消注释三参数构造函数,你会得到一个错误信息,因为构造函数是一个类成员,类成员必须在类内部声明,然后才能在类外部使用或定义。

添加行

Box(double height, double width, double length);

在你的类定义中,然后额外的构造函数也可以被编译。

关于c++ - 预期的构造函数、析构函数或 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33399721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com