ship.cpp:27:error:no matching function for call to âShip::Ship()âship.cpp:12:note:candidates are:Ship::Ship(const Ship&)ship.cpp:7:note:Ship::Ship(point,direction,int)class 里的内容是:class Ship{private:int length;direction orientati

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/11 18:17:17

ship.cpp:27:error:no matching function for call to âShip::Ship()âship.cpp:12:note:candidates are:Ship::Ship(const Ship&)ship.cpp:7:note:Ship::Ship(point,direction,int)class 里的内容是:class Ship{private:int length;direction orientati
ship.cpp:27:error:no matching function for call to âShip::Ship()â
ship.cpp:12:note:candidates are:Ship::Ship(const Ship&)
ship.cpp:7:note:Ship::Ship(point,direction,int)
class 里的内容是:
class Ship
{
private:
int length;
direction orientation;
point origin;
PointCollection points;
PointCollection hits;
public:
Ship(point originPoint,direction orientationDirection,int lengthValue);
Ship(const Ship&);
bool containsPoint(const point& p) const;
bool collidesWith(const Ship* s) const;
void shotFiredAtPoint(const point& p);
bool isHitAtPoint(const point& p);
int hitCount() const;
const Ship& operator=(const Ship& s);
};
//然后这里报错!
bool Ship::collidesWith(const Ship* s)const{
s = new Ship[s->length];
}

ship.cpp:27:error:no matching function for call to âShip::Ship()âship.cpp:12:note:candidates are:Ship::Ship(const Ship&)ship.cpp:7:note:Ship::Ship(point,direction,int)class 里的内容是:class Ship{private:int length;direction orientati
s = new Ship[s->length];是创建了一个Ship对象数组,这时候会调用Ship的构造函数,对Ship进行初始化,此时调用的是无参的构造函数,但是你的程序中没有这样的够构造函数,所以出错了.
解决办法:
1、在Ship类中,添加无参构造函数.
2、在创建对象数组时,创建有参数的.