QT Creator: undefined reference to... (methods from my Player class)

Getting these errors when I’m trying to run the code.
image

These are referring to parts of my ofApp.cpp (player_1.update() and player_1.draw() stuff like that):

void ofApp::setup(){

//Initialises the game_State as the start state.

//This will take the player to the "title screen"

game_state = "start";

//Initialises the players score at 0.

score = 0;

//Loads the FalconDragoon's sprite for the player object.

player_image.load("assets/player/falconDragoon.png");

player_1.setup(&player_image);

}

//Whitespace to help me tell where one void ends and another begins.

//--------------------------------------------------------------

void ofApp::update(){

//When the game is initially started...

if (game_state == "start") {

}

//When the game is moved into the main gameplay loop...

else if (game_state == "game") {

//Updates the player object during the gameplay loop.

player_1.update();

}

//When the game ends (game over)...

else if (game_state == "end" ) {

}

}//end void

//--------------------------------------------------------------

void ofApp::draw(){

//When the game is initially started...

if (game_state == "start") {

}

//When the game is moved into the main gameplay loop...

else if (game_state == "game") {

//Draws the player on screen.

player_1.draw();

}

//When the game ends (game over)...

else if (game_state == "end" ) {

}

}//VOid end

These are defined in my “player.h” file:

Sorry for all the posts, I’m really new and kinda struggling rn

Undefined reference means the cpp file (ie player.cpp) was not compiled. Do you have this in your project ? The h file is enough to help files like ofapp.cpp compile but you need to have the player.cpp compiled when it goes to link.

It should be in my project? Is there something wrong with this file?

Yet again I’m really really new to this

Also if helpful you may want to read a little about the compile process — you can have errors in different stages and the more you know what’s happening when the easier it may be to debug Compiling and Linking in C++ - C++ Articles

In general when I see undefined reference my first thought is, am I missing a cpp file? Sometimes with different IDEs you can add a file but it’s not set to be part of the project. (On osx I think this is “add to target”)

It’s in your project but it looks like update() has not been defined there so it’s missing that function when it goes to link

They should be defined within the player.h file here

You will need a player::update(){
}

In the cpp file. Similarly a draw function.

The h file is just a kind of “list of ingredients” but in this case, the function hasn’t really been written out anywhere. You might want to read about function definition vs declaration. It’s Similar to how ofApp h and cpp work with update and draw….

The are declared but not defined - meaning you said what the fun thing is called and what Params it takes but it hasn’t actually been implemented. Please look at ofapp.h and ofapp.cpp to see how update and draw are written in both places.

This is my ofApp.h

Yes but see also the ofApp.cpp - they are there too. That’s what’s needed in your player.cpp

Also if it helps, I’ve been following this guide, which led me to these errors. (Im mostly using it as a way to experiment with and mess around with to understand better how OF works)

https://openframeworks.cc/ofBook/chapters/cplusplus_basics.html

Sent the wrong chapter, this isit:
https://openframeworks.cc/ofBook/chapters/game_design.html

Can you check this code then ?

In particular the player.cpp seems to have the missing functions.

Oh shoot, I had no idea there was a completed version of this code available!
Thanks a bunch for this! And thanks sm for the help!

I kinda struggle to understand stuff from just reading, just gets jumbled up. I learn from tearing things apart and seeing how they work.

I think I get the main idea of what you’re saying, but I just need to see it in action and mess around to see how it works!

Thanks so much!

1 Like

after looking through the guide again, it seems any additions to player.cpp are absent from the guide. Seems they ssomehow missed it out?

I’m still getting the “undefined reference to 'player::player()” error, now pointing to here in my ofApp.h file:

At this point I’m honestly completely stumped on what’s causing the issue.

I have my voids setup in my Player.cpp file and the error no longer points to my ofApp.cpp file.

This is the compile output when I run the code:

I bet your player.h has a constructor defined but not in your cpp file similar to the previous issues. Can you please check the h file from the completed code where the constructor is not there.

Maybe the chapter code is not up to date ? You might want to compare and see if there are differences.

Thanks for all your help.

I took the Player and playercpp files from the completed code and pasted them into mine, and I believe what you just said above is the solution to the issue.

This is the sweetest blank box I’ve ever seen in my entire life! (its not loading the image for player, but at least it compiles! Should be an easy fix of directory issues)

Also the actual chapter in the guide itself is missing anything to do with Player.cpp, unless I’m super blind

1 Like