我想要这个 mov()
虚拟成员函数来 get()
坐标 x,y
然而,这个错误根本没有意义
pirate.o:: 在函数“ZN6Pirate3movEPS_PA100_3Sea”中:|
pirate.cpp:: 对 `Ship::getX() const' 的 undefined reference (第 7 行)
getX()是继承的
父类:Ship,派生类:Pirate
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
发货.cpp
#include "Ship.h"
int Ship::getX()const
{return x;}
int Ship::getY()const
{return y;}
virtual void func()=0;
virtual void mov()=0;
海盗.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
海盗.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();
}
海.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
最佳答案
另一个答案完全正确。如果您删除这两行,它将正确编译,并且您收到的错误(以及您没有收到其他错误的事实)是由于编译过程中未正确包含 Ship.cpp。
我不确定您是否可以访问 Visual Studio 2013,但如果可以,我使用以下解决方案/项目对其进行了测试:http://filebin.ca/1i9z9TwF2kf5/Pirates.zip
关于c++ - 未定义对成员函数的引用(错误的意外错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27081903/