I am setting up the framework for an SFML game in Code::Blocks, but I ran into some trouble. I get the error "13 undefined reference to 'Game::onInit().'"
main.cpp
[code]
#include "game.h"
int main()
{
Game game;
return EXIT_SUCCESS;
}
[/code]
I try to keep things simple. Here is game.h:
[code]
#ifndef _GAME_
#define _GAME_
#include <SFML/Graphics.hpp>
#include <iostream>
class Game{
public:
Game(){
Running = false; // DON'T FORGET TO CHANGE BACK
onInit(); // <----- THIS IS LINE 13
while(Running){
}
}
~Game(){}
private:
int onInit();
int onEvent();
int onLoop();
int onRender();
int onCleanup();
bool Running;
};
#endif // _GAME_
[/code]
All member functions are declared, but are actually found in separate files of their own. (I'm following an SDL tutorial)
This is onInit.cpp:
[code]
#include "game.h"
int Game::onInit(){
return 0;
}
[/code]
Every file, including onInit.cpp, is in the same project as game.h and main.cpp. Does anyone have any suggestions? I haven't touched C++ for quite some time, and I probably have no idea what I'm doing. All help is appreciated.
You need to prototype the method Game::onInit before using it.
Change the constructor to a simple prototype in your class definition, then define it in the actual C source after everything else has been prototyped.
[QUOTE=ROBO_DONUT;31697127]You need to prototype the method Game::onInit before using it.
Change the constructor to a simple prototype in your class definition, then define it in the actual C source after everything else has been prototyped.[/QUOTE]
I thought I had prototyped it. If I take the code in onInit.cpp and place it beneath the Game class, but above #endif, it works just fine. I just want to let the compiler know to look in onInit.cpp for the implementation.
Also, what's wrong with the constructor?
I think it might just be some sort of linking problem, or something. If not, could I trouble you for an example? Thanks for the reply.
If it helps, I am trying to follow this file structure: [url]http://www.sdltutorials.com/sdl-tutorial-basics/[/url]
[QUOTE=Homez;31697832]I thought I had prototyped it. If I take the code in onInit.cpp and place it beneath the Game class, but above #endif, it works just fine. I just want to let the compiler know to look in onInit.cpp for the implementation.
Also, what's wrong with the constructor?
I think it might just be some sort of linking problem, or something. If not, could I trouble you for an example? Thanks for the reply.[/QUOTE]
What he means with the constructor is you want to make the header file be the prototypes and a separate file for the definitions. The main reason for this is because it's much easier to read (the header acts like an index). It should look something like this:
class.h
[cpp]
//include stuff
class ClassThing
{
private:
//private stuff
public:
//public stuff
ClassThing();
~ClassThing();
};
[/cpp]
class.cpp
[cpp]
#include "class.h"
ClassThing::ClassThing()
{
//constructor stuff here
}
ClassThing::~ClassThing()
{
//destructor stuff here
}
[/cpp]
-snip-
I'm sorry guys, I found the issue. I was right, the project files weren't listed as search targets. I'm very new to Code::Blocks. I used to use Dev-C++ until I got laughed at.
I really do appreciate all the help, and I'll decentralize my game.h file.
In case anyone else has this issue:
Open C:B.
Project > Properties > Build Targets. Near there bottom is a window "Build Target Files." Check all the boxes.
Sorry, you need to Log In to post a reply to this thread.