C++构造函数
程序代码:
#include<iostream> using namespace std; class Box{ private: double length; double breadth; double height; public: Box(double len, double bre, double hei);//这是构造函数。 Box(){};//请问,这是什么函数? //若没有这句,错误有两个,在下方已标出。 //若没有{},也会出现错误。 double GetVolume(void); Box operator+ (const Box&); }; Box::Box(double len, double bre, double hei){ length = len; breadth = bre; height = hei; } double Box::GetVolume(void){ return length * breadth * height; } Box Box::operator+ (const Box& obj){ Box box;//错误一:[Error] no matching function for call to 'Box::Box()' box.length = this->length + obj.length; box.breadth = this->breadth + obj.breadth; box.height = this->height + obj.height; return box; } int main(void) { Box box1(2.0, 4.0, 6.0); Box box2(1.0, 2.0, 3.0); Box box3;//错误二:[Error] no matching function for call to 'Box::Box()' double Volume = 0.0; Volume = box1.GetVolume(); cout << "BOX1'Volume is " << Volume << endl; Volume = box2.GetVolume(); cout << "BOX2'Volume is " << Volume << endl; box3 = box1 + box2; Volume = box3.GetVolume(); cout << "BOX3'Volume is " << Volume << endl; return 0; }