Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;构造问题 我很久没有使用C++了,我从来没有真正掌握过好的课程。 我决定通过制作一个小的几何应用程序来重新学习课程。 这里是正方形。h: class Square{ public: float width; float height; float area; float perimeter; void Square(int,int); void Square(); void ~Square(); };_C++_Class_Constructor_Return Type - Fatal编程技术网

C++;构造问题 我很久没有使用C++了,我从来没有真正掌握过好的课程。 我决定通过制作一个小的几何应用程序来重新学习课程。 这里是正方形。h: class Square{ public: float width; float height; float area; float perimeter; void Square(int,int); void Square(); void ~Square(); };

C++;构造问题 我很久没有使用C++了,我从来没有真正掌握过好的课程。 我决定通过制作一个小的几何应用程序来重新学习课程。 这里是正方形。h: class Square{ public: float width; float height; float area; float perimeter; void Square(int,int); void Square(); void ~Square(); };,c++,class,constructor,return-type,C++,Class,Constructor,Return Type,下面是square.cpp: #include "square.h" Square::Square (int w, int h){ width = w; height = h; area = width * height; perimeter = (width*2)+(height*2); } Square::Square (){ } Square::~Square (){ } 当我运行/构建程序时,它会显示错误:构造函数的返回类型规范无效 我想这是说

下面是square.cpp:

#include "square.h"

Square::Square (int w, int h){
    width = w;
    height = h;
    area = width * height;
    perimeter = (width*2)+(height*2);
}

Square::Square (){

}

Square::~Square (){

}
当我运行/构建程序时,它会显示
错误:构造函数的返回类型规范无效

我想这是说构造函数和析构函数应该是除
void
之外的其他东西,但我认为我错了

我猜这是说构造函数和析构函数应该是除
void

是的,应该是:

Square(int,int);
Square();
~Square();
我想
void
意味着函数不返回任何内容

是的,但这些不是函数。它们是构造函数和析构函数,不需要指定的返回类型

我猜这是说构造函数和析构函数应该是除
void

是的,应该是:

Square(int,int);
Square();
~Square();
我想
void
意味着函数不返回任何内容


是的,但这些不是函数。它们是构造函数和析构函数,不需要指定的返回类型。

去掉构造函数和析构函数中的
void

Square(int,int);
Square();
~Square();

还有一个建议,因为你正在学习。如果您没有考虑将类变量公开给子类,请将它们设置为私有

去掉构造函数和析构函数中的
void

Square(int,int);
Square();
~Square();

还有一个建议,因为你正在学习。如果您没有考虑将类变量公开给子类,请将它们设置为私有

在构造函数和析构函数中应该根本没有返回类型

class Square
{
public:
    float width;
    float height;
    float area;
    float perimeter;


    Square(int,int);
    Square();
    ~Square();

};

在构造函数和析构函数中,应该根本没有返回类型

class Square
{
public:
    float width;
    float height;
    float area;
    float perimeter;


    Square(int,int);
    Square();
    ~Square();

};

构造函数
析构函数
没有返回类型。它们是类的一种特殊函数,与
同名

class Square{
public:
    float width;
    float height;
    float area;
    float perimeter;


    Square(int,int);
    Square();
    ~Square();




};

构造函数
析构函数
没有返回类型。它们是类的一种特殊函数,与
同名

class Square{
public:
    float width;
    float height;
    float area;
    float perimeter;


    Square(int,int);
    Square();
    ~Square();




};

非常感谢。我认为
void
意味着函数不返回任何内容?@Awalrod是的,但它们不是函数。他们是构造函数和析构函数。谢谢!我认为
void
意味着函数不返回任何内容?@Awalrod是的,但它们不是函数。它们是构造函数和析构函数。为什么宽度和高度存储为浮点时构造函数参数为int?为什么宽度和高度存储为浮点时构造函数参数为int?