【问题标题】:expected constructor, destructor or header预期的构造函数、析构函数或标头
【发布时间】:2015-10-28 19:17:30
【问题描述】:

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

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

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

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;

}

有人知道为什么吗?

【问题讨论】:

  • 粘贴exact错误信息。
  • “本节”所做的是提供三参数构造函数的主体。它不是默认构造函数——可以不带参数调用它们。可悲的是,它也不是好的 C++ 风格,看起来像是 Java 程序员做的。
  • 1.使用初始化列表。 2.不要写这样的代码。它损害了封装的所有原则。 IMO,在这里使用命名空间会更合适。不过,作为一个例子,它没关系。 3. 你的 setter 不接受任何参数。
  • 您听说过const 关键字吗?阅读相关内容可能很有用
  • 它声明 - “Box::setLength(double&)”没有拟合函数 - 候选者是“double Box::setLength()

标签: c++


【解决方案1】:

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

添加行

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

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

【讨论】:

  • 最后一行的定义应该是声明吗?
  • @EdHeal:不,我的意思是“类定义”。
  • @EdHeal:是的,我确定。 class Box { ..... }; 是一个类型定义。一个类声明可以是class Box;,我不是那个意思。
  • 这似乎无法回答问题。
  • @EdHeal IMO,例如一个类声明是class Box;
【解决方案2】:

C++ 有一些奇怪的行为:如果你没有声明和定义一个默认构造函数,它会为你做。认为自动生成。它还将定义一个编译器生成的复制构造函数和析构函数。理解这一点会很有帮助,因为无论你定义与否,这些东西都存在。

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

public:
    Box();

并在cpp文件中定义:

Box::Box()

你正确地声明和定义了这个构造函数。

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

public:
    Box();
    Box(double height, double width, double length); // this is new

然后你可以在cpp文件中取消注释你的定义,一切都应该很好。

另一个风格点:您定义 3 参数构造函数的方式不是很好的风格。发生的情况是您构造了一个 Box,当您这样做时,您使用默认构造函数,因为它的成员变量是高度、宽度和深度。然后调用 3 个成员函数来分配这些变量。在 C++ 中,您可以通过使用初始化列表来避免这一切。您的 3 参数构造函数的主体变为

Box::Box(double height, double width, double length) :
    height(height), width(width), length(length)
{}

这句话的意思是“为我构建一个盒子,当你这样做时,使用传入的高度值作为成员高度,宽度作为成员宽度,长度作为成员长度”。通过像使用这些值一样“开箱即用”地构建 Box,您可以将 0 的赋值保存为成员变量和 3 个函数调用的默认值。这对您的成员变量使用复制构造函数,而不是使用它们的默认构造函数,然后进行赋值。

(技术说明:作为内置函数,双打在技术上没有这些构造函数,但语义表现得好像它们有,所以你可以将它们视为一阶思考。)

这意味着,另外,如果你定义了一个复制构造函数:

Box(const Box& other);

然后你可以在其他类中使用它来在它们的初始化列表中初始化一个 Box,例如

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

它将使用 Box 类中的复制构造函数为 BunchOfBoxes 执行相同类型的初始化构建

【讨论】:

  • 其实“默认构造函数”中的“默认”是指“可以不带参数调用”。你的定义适合“默认”,可以适用于默认构造函数、复制构造函数、复制赋值运算符等。
【解决方案3】:

您的类声明/定义有问题: 你的设置器应该有参数,所以你可以将它们用作设置器。 在你的 Box.hpp 中更改

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

 double setHeight(double _height); 
 double setWidth(double _width);
 double setLength(double _length);

然后在你的 Box.cpp 中更改

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

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-04
    • 2010-12-12
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多