-1

I am currently very new to c++, i have started learning how to use pointers in a path finding algorithm.

I am having an issue with calling a function within a class that is derived from a base class.

The specific piece of code causing issue is:

FreeTile *tempPointer = new FreeTile();
cout<<tempPointer->getFree()<<endl;
mapp[i][j] = tempPointer;

when i call getFree (which returns a boolean value) i get the error:

undefined reference to Tile::getFree(). Tile being the base class.

The header for FreeTile is:

#ifndef FREETILE_H
#define FREETILE_H
#include "Tile.h"

class FreeTile:public Tile
{
    public:
        FreeTile();
        virtual ~FreeTile();
        void setParent(FreeTile* par);
        int getF();
        int getG();
        int getH();
        void setF(int in);
        void setG(int in);
        void setH(int in);
        FreeTile* getParent();
    protected:
    private:
        int F;
        int G;
        int H;
        bool free;
};

Tile header is: #ifndef TILE_H #define TILE_H

class Tile
 {
    public:
        Tile();
        virtual ~Tile();
        bool getFree();
        void setFree(bool bo);
    protected:
    private:
        bool free;
};

#endif // TILE_H
#endif // FREETILE_H

Finally the cpp file for Tile:

#include "Tile.h"
#include <iostream>
using namespace std;

bool free;


Tile::Tile()
{
    cout<<"Constructor Called"<<endl;
}

Tile::~Tile()
{
    //dtor
}

bool getFree(){
    return free;
}

void setFree(bool bo){
    free = bo;
}

If you need more code or if im missing something blatant feel free to shame me as much as you like :P

Thanks in advance.

On a side note, can you initiate a private variable in a constructor such as free = true as when doing this it states the variable is private.

NAN
  • 3
  • 2
  • There's a lot going wrong there. You've defined `free` as a global variable, and defined `getFree` and `setFree` as global variables. – Sneftel Nov 18 '14 at 21:45
  • 1
    Your variable `free` in `FreeTile` will hide the variable `free` from `Tile` and is probably not what you're looking for. If you want access to `free` from `Tile` in `FreeTile` consider making it `protected` in `Tile` instead of `private` and not declaring it at all in `FreeTile`. – YoungJohn Nov 18 '14 at 21:48
  • Probably better still to have `FreeTile` access the `private` `free ` via `getFree` and `setFree`like everyone else. – YoungJohn Nov 18 '14 at 21:49
  • I've dropped the free variable from FreeTile so it is only in the Tile class and i also changed it to protected. Though i can see how this is beneficial i still get the 'undefined reference to` error when i call getFree(). Also in the constructor for FreeTile i had the method setFree but i got the same error as the other. I also apologise for the poor code in it's self, im still in the early stages of understanding C++ as i am used to the somewhat simpler Java – NAN Nov 18 '14 at 21:57
  • You also have to address the concerns listed by @Sneftel – YoungJohn Nov 18 '14 at 22:02
  • Ah, sorry managed to not see it, this is where the beginnerness comes in, firstly is free a global variable because it's defined as public? and also does this mean setFree and getFree arent being treated as methods, and if so how do i define them as such? – NAN Nov 18 '14 at 22:07

2 Answers2

1

In the Cpp file rename "bool getFree()" to "bool Tile::getFree()"

In your implementation the function is just a regular c gloabl function. In the fixed version it is the class function implementaion of the function you declare in the header file

Also 1st in your Tile you have a private variable "bool free" in the cpp file you have a global variable "bool free" this is confusing. Probably want to delete the one you declared in the cpp file.

Want a deeper explanation?

  • Thanks very much. Fixed the dual free in the different classes. And i placed the Tile:: in front and my problem is fixed thankyou very much :) Hit me with a deeper explanation, wouldn't be a good learner if i didn't – NAN Nov 18 '14 at 22:32
0

Yeah! my 1st answer! Deeper Explanation: the function you declared in the Class Tile is not defined (just declared) because you didn't add "Tile::" before the function definition in the cpp file (i.e you didn't define a scope). The function you wrote in the cpp file is both defined and declared in the cpp file, so only functions written after it in the cpp file can call it (works same a c). Probably when you wrote the function it didn't know that "free" was, right? because it was not a class function. so you added the global "bool free" but that is a completely different variable. Glad to help! don't forget to mark this as answered!