Issues calling base class function from derived class

165 views Asked by At

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.

2

There are 2 answers

1
alonewenson On BEST ANSWER

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?

0
alonewenson On

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!