C++构造函数问题
C++ constructor problems
我已经很久没有使用c++了,我从来没有真正很好地掌握过类
我决定通过制作一个小型几何应用程序来重新学习课程
这是正方形。h:
class Square{
public:
float width;
float height;
float area;
float perimeter;
void Square(int,int);
void Square();
void ~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 (){
}
当我运行/构建程序时,它显示error: return type specification for constructor invalid
我想这是说构造函数和析构函数应该是void
以外的东西,但我认为我错了。
我想这是说构造函数和析构函数应该是
void
之外的东西
是的,应该是:
Square(int,int);
Square();
~Square();
我认为
void
意味着函数不返回任何内容?
是的,但这些不是函数。它们是构造函数和析构函数,不需要指定的返回类型。
void
。
Square(int,int);
Square();
~Square();
还有一个建议,因为你正在学习。如果您不想将类变量公开给子类,请将它们设为私有变量。
在构造函数和析构函数中根本不应该有返回类型,
class Square
{
public:
float width;
float height;
float area;
float perimeter;
Square(int,int);
Square();
~Square();
};
Constructor
和destructor
没有返回类型。它们是类的一种特殊函数,具有和class
相同的名称。
class Square{
public:
float width;
float height;
float area;
float perimeter;
Square(int,int);
Square();
~Square();
};
相关文章:
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- constexpr构造函数需要常量成员函数时出现问题
- 有关 c++ 构造函数的问题
- 关于复制构造函数的一个棘手问题
- 复制构造函数隐式转换问题
- 构造函数 (C++) 中的 char 指针参数存在问题
- C++:关于使用 Stroustrup 示例移动构造函数/赋值的问题
- 关于 std::bitset 构造函数的几个问题?
- 构造函数和 G++ 编译配方的问题
- 在 boost::variant 中类 holden 的复制构造函数存在问题
- C++ 关于两个类构造函数的问题
- 在构造函数 c++ 中初始化属性时出现问题
- 空构造函数和值传递问题
- 我是否需要在虚拟继承类的构造函数中初始化基类以解决菱形继承问题?
- 复制和赋值构造函数的问题
- 变分模板递归构造函数问题
- C++中用户定义的构造函数出现问题
- C ++回测问题:如何检查构造函数在假定失败时是否失败(给定输入的无效参数)
- C++的抽象类继承和构造函数的问题
- 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?