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/06/24 23:22:29
![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](/uploads/image/z/13086097-25-7.jpg?t=ship.cpp%3A27%3Aerror%3Ano+matching+function+for+call+to+%26%23226%3BShip%3A%3AShip%28%29%26%23226%3Bship.cpp%3A12%3Anote%3Acandidates+are%3AShip%3A%3AShip%28const+Ship%26%29ship.cpp%3A7%3Anote%3AShip%3A%3AShip%28point%2Cdirection%2Cint%29class+%E9%87%8C%E7%9A%84%E5%86%85%E5%AE%B9%E6%98%AF%EF%BC%9Aclass+Ship%7Bprivate%3Aint+length%3Bdirection+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 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、在创建对象数组时,创建有参数的.