我想這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