undefined reference to a function

header file:
 
const sf::Font& GAME_FONT();


source file:
1
2
3
4
5
6
sf::Font FONT;
const sf::Font& GAME_FONT()
{
FONT.loadFromFile("arial.ttf");
return FONT;
}


i get undefined reference error when im calling GAME_FONT() function.
how do i fix this?
thanks.
Hello,
Can you post full error logs from your compiler please?


In function `ZN4MenuC2Ev':|
undefined reference to `GAME_FONT()'|
undefined reference to `GAME_FONT()'|
undefined reference to `GAME_FONT()'|
|undefined reference to `GAME_FONT()'|
more undefined references to `GAME_FONT()' follow|
||error: ld returned 1 exit status|


Ok, post the whole header file that contains your function declaration (GAME_FONT())
okay sorry.
1
2
3
4
5
6
7
8
9

#ifndef CONFIG_H_INCLUDED
#define CONFIG_H_INCLUDED

#include <SFML/Graphics.hpp>

const sf::Font& GAME_FONT();

#endif // CONFIG_H_INCLUDED 
Last edited on
Try adding the keyword extern before your function declaration.

extern const sf::Font& GAME_FONT();
@ne555
i dont see anything that matched my problem :(


You did not define the thing in the error message

i defined the prototype exactly


You forgot to link the file that defines it

ive been using my IDE for long time so it cant be that



What IDE are you using?
Is it a big project?
What are libraries involved in your project?
Is your header that causes the (linker) error included by multiple source files?
Make changes to the function so that the function becomes generic, and it can potentially eliminate the problem. This is a temporary measure, so inform us if this works.

const string GAME_FONT(); // header file

const string GAME_FONT() {return "Good.";} // source file
guys i swear i added the source file into the project!, and i tried that again and it works.
Good to hear :)
but as soon as i call the function my program dont move, its like its not moving from that line which the GAME_FONT() is called.
but in my other file, it works fine when i call the GAME_FONT();
In other words, your program is stuck and not responding?
In other words, the function works ok in your other programs?
no its not, i put std::cout in all my functions and it outputs as expected in the console but my game window wont render all of my game objects,( the game is really not freezing or not responding.)

look at this code:
1
2
3
4
Game::Game()
{
 GAME_FONT();
}

the problem starts when i call the GAME_FONT.

when i removed the GAME_FONT() my game behaves fine, it renders all of my game objects.


but when i call that in other source file it behaves fine.

1
2
3
4
Menu::Menu()
{
 GAME_FONT(); // my game behaves fine
}


this is a super strange bug
Last edited on
Ok, show us the full content of your source file "Game.cpp" please.

> The problem starts when I call the GAME_FONT().
Has the function been changed or it still stays original? What does your program behave like when the function is called in Game::Game()?
> But my game window won't render all of my game objects
Can you list them?
http://pastebin.com/U5BKYmqW
heres the code of the 2 source file im telling.


Has the function been changed or it still stays original?
1
2
3
4
5
6
sf::Font dataFont = sf::Font();
const sf::Font& GAME_FONT()
{
    dataFont.loadFromFile("arial.ttf");
    return dataFont;
}



What does your program behave like when the function is called in Game::Game()?


when in INMENU state (menu) it doesnt render all the text in my menu.
when in INGAME state (playing) no problem



ive put some comments in the pastebin

EDIT:
when i try to render the resume while in INMENU state, the texts are rendering (its like it triggers something) but its strange looking fonts.
https://s31.postimg.org/dpcmgb00b/wqeqweqweqweqwqweqwe.jpg
the text is supposed to be "are you sure you want to exit?"
Last edited on
So without GAME_FONT(), your program can't display text at all?
Are you using global objects?
Topic archived. No new replies allowed.