C++ 应为构造函数、析构函数或标头

C++ 应为构造函数、析构函数或标头,c++,C++,这个程序包含三个部分:标题、类的函数和交互的主要部分。但是,它不会编译 我不断得到响应,有一个预期的构造函数、析构函数或类型转换 #ifndef BOX_H #define BOX_H class Box { private: double height; double width; double length; public: Box(); double setHeight(); d

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

我不断得到响应,有一个预期的构造函数、析构函数或类型转换

#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  
function.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;
}
#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;

}
main.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;
}
#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.hpp”//包含Box类声明
使用名称空间std;
int main()
{
长方体;
双波长;
双锥度;
双倍重量;
cout Alength;
不能>宽度;
cout>ahight;
对象。设置长度(长度);
如果(!object.setWidth(Awidth))

cout如果取消对三参数构造函数的注释,将收到一条错误消息,因为构造函数是类成员,类成员必须在类内部声明,才能在外部使用或定义

添加行

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

C++有一些奇怪的行为:如果你不能声明和定义一个默认构造函数,它会帮你完成。想想autogenerated。它还会定义一个编译器生成的复制构造函数和析构函数。理解这一点很有帮助,因为无论你定义与否,事物都是存在的

你们都在标题中声明了构造函数:

public:
    Box();
并在cpp文件中定义:

Box::Box()
您正确地声明和定义了此构造函数

如果需要任何其他构造函数,则必须先声明它,然后才能定义它

public:
    Box();
    Box(double height, double width, double length); // this is new
然后,您可以在cpp文件中取消注释您的定义,一切都应该很好

另一种风格点:定义3个参数构造函数的方式不是很棒的样式。发生的是,你构建一个框,当你这样做时,你使用默认构造函数来为其成员的高度、宽度和深度变量。然后调用3个成员函数来分配这些变量。C++中,你可以通过使用一个初始值设定项列表。3参数构造函数的主体变为

Box::Box(double height, double width, double length) :
    height(height), width(width), length(length)
{}
这句话的意思是“为我构建一个框,这样做时,使用传入的高度值表示成员高度,宽度值表示成员宽度,长度值表示成员长度”。通过构建框“开箱即用”,可以将赋值0保存为成员变量和3个函数调用的默认值与这些值一样。这对成员变量使用复制构造函数,而不是在赋值后使用默认构造函数

(技术说明:作为内置函数,Double在技术上没有这些构造函数,但是语义的行为就好像它们有一样,因此您可以将它们视为一阶构造。)

此外,这意味着,如果定义了复制构造函数:

Box(const Box& other);
然后,您可以在其他类中使用它来初始化其初始值设定项列表中的框,例如

BunchOfBoxes(const Box& firstBox) :
    m_firstBox(firstBox)
{}

它将使用Box类中的复制构造函数对BunchofBox进行相同类型的构建初始化

您的类声明/定义有问题: 您的setter应该具有参数,以便您可以将它们用作setter。 在你的盒子里。换hpp

 double setHeight(); 
 double setWidth();
 double setLength();

然后在你的盒子里。改变

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

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

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


粘贴准确的错误消息。什么是“此部分”DO是一个三参数构造函数的主体。它不是默认的构造函数——那些可以被调用的没有参数。很遗憾,它也不是好的C++风格,看起来像java程序员那样做。1。使用初始化列表。2。不要写这样的代码。它损害了所有的封装原则。O.作为一个例子,这没关系。3.你的setters不接受任何参数。你听说过
const
关键字吗?读一下它可能会有用,它说,“Box::setLength(double&)”没有合适的函数-候选函数是“double Box::setLength(),最后一行的定义应该是声明吗?@EdHeal:不,我的意思是”类定义“@EdHeal:是的,我确定。
class-Box{….};
是一个类型定义。类声明可以是
class-Box;
,我不是这个意思。这似乎并没有回答这个问题。@EdHeal-IMO,类声明是
class-Box;
例如。实际上,“默认构造函数”中的“默认”是指您的定义符合“defaulted”,可以应用于默认构造函数、复制构造函数、复制赋值运算符等。