So im just trying to understand a simple program in C++ but i keep getting an error when trying to compile this code. The error is [Linker error] undefined reference to `Player::getHeight()' . So i have my main code in one file called Main.cpp:

#include "Player.h"


int main()
{
    Player a;
   cout << a.getHeight() << endl;
     system("pause");
    return 0;
}

then i have my header file in Player.h :

#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
using namespace std;

class Player
{
      public:
      Player(){height = 0;};
                
      int getHeight();
                  
      private:
              int height;
};
#endif

then i have my function file Player.cpp :

#include "Player.h"


int Player::getHeight()
{
 return height;
}

Not exactly sure what is going on here, moving things into header files is new to me. Thanks for your time.

I copied exactly what you have and I didn't get any errors.

What compiler do you use?

Im using bloodshed dev for C++, so you have my exact code in three different files like i listed?

Yeah and I use dev C++ too.

Trying hitting the rebuild all button it normally fixes lots of errors for me after I change a lots of code.

Also I see that you are using spaces to indent your code go Tools >> Editor Options and check Use Tab Character.

This makes it a lot cleaner and you just have to hit tab and it gives you a perfect indent.

Ya im still getting::
[Linker error] undefined reference to `Player::getHeight()'

I even erased everything and copied the code back in.... I mean if i put the return statement back into my .h file it works but that defeats the purpose of having function prototypes. I just dont get what im doing wrong.

Thanks for the tip about the tab, that was getting annoying

Im using all default settings as well. Im not sure if that matters.

I was able to get the same error as you by deleting the "player.cpp" file making it so only the prototype exists.

The only thing I can think of is is the "player.cpp" file part of the project? Sounds stupid but that would give you the error.

Wow man... so i didnt have it as a project. I just had all 3 files that i created, not set together as a project. Never in a million years did i think that would matter. I make a project and guess what... it works. Only took me 8 hrs to figure that out.. maybe now i can start on my actual HW lol.. Thanks for your help!

No problem. Good luck with the homework.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.