-1

I would like this mov() virtual member function to get() the coordinates x,y
However there is this error which doesn't make sense at all
pirate.o:: In function 'ZN6Pirate3movEPS_PA100_3Sea':|
pirate.cpp:: undefined reference to `Ship::getX() const' (line 7)

getX() is inherited
Parent Class:Ship, Derived class: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

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
solid.py
  • 2,782
  • 5
  • 23
  • 30
  • 1
    If you got a link error, that is not the code you're compiling. There are quite a few errors before that stage. – molbdnilo Nov 22 '14 at 20:09
  • @molbdnilo There are pure virtuals, passing the same object of the class,probably scope issues with inherited getX() why should it be a linker error? – solid.py Nov 22 '14 at 20:12
  • 1
    The first line of C++ in ship.h is `private:` so you're missing something in the code you've shown. `ship.h` tries to include `sea.h` (why?) which tries to include `ship.h` (again, why?) - that's not going to work. You also need to show your compilation command if you want an answer to why it fails. – Jonathan Wakely Nov 22 '14 at 20:16
  • @niCk The error you posted is from the linker, that's why it's a linker eror. – molbdnilo Nov 22 '14 at 20:17
  • @molbdnilo btw I use Codeblocks v13.12 – solid.py Nov 22 '14 at 20:19
  • @JonathanWakely I want to pass a Sea 2d array as an argument – solid.py Nov 22 '14 at 20:24
  • 1
    @niCk, you don't need to include any header to do that (you can just declare `class Sea`) but you definitely don't need circular includes, because they don't even work. Two headers cannot include each other like that. – Jonathan Wakely Nov 22 '14 at 20:37
  • dont know if anyone said, but you have Class Ship, not class ship – Creris Nov 22 '14 at 20:55
  • @Creris I was wondering if you can give a better answer than Wakely thanks in advance – solid.py Nov 22 '14 at 20:56
  • 2
    I assure you that if the code you've shown above is what you're trying to compile, my answer is correct. Try figuring out why Ship.cpp is not being compiled instead of waiting for a different answer. – Jonathan Wakely Nov 22 '14 at 20:59
  • Don't forget a virtual destructor. – Neil Kirk Nov 22 '14 at 21:54
  • Why does your formatting differ wildly between files? – Lightness Races in Orbit Nov 22 '14 at 21:58

2 Answers2

3

You forgot to compile and link Ship.cpp

I can tell this because if you'd tried to compile it then you'd have got an error here:

virtual void func()=0;
virtual void mov()=0;

That's not valid C++, you don't define pure virtuals like that (you don't need to define them at all unless they are destructors or you call them explicitly)

If you don't link with the file with the definition of Ship::getX() const then it's not surprising that the linker tells you it's undefined.

solid.py
  • 2,782
  • 5
  • 23
  • 30
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • The line `virtual void func()=0;` by itself is valid Example http://stackoverflow.com/questions/1306778/c-virtual-pure-virtual-explained?lq=1 – solid.py Nov 22 '14 at 20:29
  • 1
    Erm, no, that line by itself is only valid **inside** a class body, not at file scope as you have it in `Ship.cpp`. I have no idea what you think that link is an example of. You need to tell you IDE how to add extra files to the project. – Jonathan Wakely Nov 22 '14 at 20:34
  • all files are added to the project, I can see them under my Project management Tab – solid.py Nov 22 '14 at 20:48
3

The other answer is exactly right. If you remove those two lines it will compile correctly, and the error you're getting (and the fact that you're not getting other errors) is due to the fact that Ship.cpp is not properly included in the compilation process.

I'm not sure if you have access to Visual Studio 2013, but if you do, I tested it to be sure with the following solution/project: http://filebin.ca/1i9z9TwF2kf5/Pirates.zip

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Amadeus
  • 416
  • 3
  • 7
  • @Amadeus I compiled my entire project (not just pirate.cpp ) with Visual Studio 2013 and Dev-C++ And It build with no compiler/linker erros Thanks a lot! – solid.py Nov 23 '14 at 17:21