我想这mov()
虚成员函数get()
的坐标x,y
但是有这个错误,完全不
pirate.o意义: :函数'ZN6Pirate3movEPS_PA100_3Sea':|
pirate.cpp ::未定义参考`船舶::的getX()const的”(第7行)
的getX()被继承
父类:船舶,派生类:海盗未定义的引用成员函数(假意外错误)
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <iostream>
#include "sea.h"
#define SIZE 100
class Ship
{
private:
int x,y; //Coordinates of ship
public:
Ship(){}
virtual void func()=0;
virtual void mov()=0;
protected:
int getX()const;
int getY()const;
};
#endif
Ship.cpp
#include "Ship.h"
int Ship::getX()const
{return x;}
int Ship::getY()const
{return y;}
virtual void func()=0;
virtual void mov()=0;
Pirate.h
#ifndef PIRATE_H
#define PIRATE_H
#include "ship.h"
#include "sea.h"
class Pirate : public Ship
{
protected:
void func();
void mov(Pirate * ship , Sea Map[SIZE][SIZE]);
};
#endif
Pirate.cpp
#include "pirate.h"
void Pirate::func(){}
void Pirate::mov(Pirate* ship , Sea Map[SIZE][SIZE])
{
int x_ref = ship->getX();
int y_ref = ship->getY();
}
Sea.h
#ifndef SEA_H
#define SEA_H
#include "ship.h"
class Sea
{
private:
bool hasShip;
public:
Sea(){hasShip=0;}
bool gethasShip()const{return hasShip;}
void sethasShip(bool i){hasShip = i;}
};
#endif
如果你有一个链接错误,就是不你正在编译的代码。那个阶段之前有很多错误。 – molbdnilo 2014-11-22 20:09:41
@molbdnilo存在纯虚拟,传递类的相同对象,可能是继承getX()的范围问题为什么它应该是链接器错误? – hitter 2014-11-22 20:12:36
ship.h中的C++的第一行是'private:',所以你在代码中缺少了一些东西。 'ship.h'试图包含'sea.h'(为什么?),它试图包含'ship.h'(再次,为什么?) - 这是行不通的。如果您想要回答失败的原因,您还需要显示编译命令。 – 2014-11-22 20:16:14