应为构造函数、析构函数或标头
expected constructor, destructor or header
这个程序包含三个部分:标题、类的函数和交互的主要部分。但是,它不会编译。
我不断得到的响应是,有一个预期的构造函数、析构函数或类型转换。
#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;
}
有人知道为什么吗?
如果取消注释三参数构造函数,您将收到一条错误消息,因为构造函数是类成员,必须在类内部声明类成员,然后才能在外部使用或定义它们。
添加行
Box(double height, double width, double length);
在类定义中,然后也可以编译额外的构造函数。
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个函数调用的默认值。这将使用成员变量的复制构造函数,而不是后面跟着赋值的默认构造函数。
(技术说明:作为内置程序,从技术上讲,doubles没有这些构造函数,但语义表现得就像它们有一样,所以你可以把它们看作是一阶思维。)
这意味着,另外,如果您定义了一个复制构造函数:
Box(const Box& other);
然后你可以在其他类中使用它来初始化它们的初始化器列表中的Box,例如
BunchOfBoxes(const Box& firstBox) :
m_firstBox(firstBox)
{}
它将使用Box类中的复制构造函数在构建BunchOfBoxes 时进行相同类型的初始化
您的类声明/定义有问题:您的setter应该有参数,这样您就可以将它们用作setter。在您的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;
}
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 在c++中使用向量时,如何调用构造函数和析构函数
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 不命名构造函数和析构函数上的类型错误
- 在 UML 类图中为C++类添加构造函数和析构函数
- 基类中的默认析构函数禁用子类中的移动构造函数(如果有成员)
- 为什么类构造函数为其成员生成析构函数?
- makefile对我的名称空间对象/函数/构造函数的不确定引用
- 没有可调用的匹配函数(构造函数),候选函数不同于对给定类型的引用
- C++库制作,隐藏在标头中没有友元的析构函数/构造函数
- 从另一个成员函数/构造函数调用C++构造函数是否执行初始值设定项列表
- 在另一个函数/构造函数 [C++] 中初始化后使变量只读
- std::调用函数时找不到函数构造函数
- 显式调用基类析构函数/构造函数合法吗
- 函数构造函数和nullptr
- 用委托构造函数构造函数
- 虚函数构造函数和调用虚函数时会发生什么
- 一些Qt函数/构造函数参数的内存管理
- 使用for_each或std::transform时,c++函函数构造函数是如何被调用的?
- std函数构造函数初始化