开发者

C++ - Undefined reference to an object

Compiler: MinGW

IDE: Code::Blocks

Platform: Windows XP

External libraries: SDL, SDL_image

What I'm trying开发者_开发知识库 to do: Cap the framerate at 60 frames per second using a timer that resets after each loop.

Just a warning, I AM inexperienced with C++, but I've done research and I can't find this kind of error anywhere else. I'm sure it has to do with scope, but I'm not sure how to resolve it.

Compiler output:

In function ZN4data6OnLoopEv':|'C:\Documents and Settings\Evan\My Documents\Programming Projects\Roguelike SDLtest 2\OnLoop.cpp|5|undefined reference to 'fps'

C:\Documents and Settings\Evan\My Documents\Programming Projects\Roguelike SDLtest 2\OnLoop.cpp|7|undefined reference to 'fps'

C:\Documents and Settings\Evan\My Documents\Programming Projects\Roguelike SDLtest 2\OnLoop.cpp|9|undefined reference to 'fps'

Main.cpp:

#include "data.h"
bool data::OnExecute()
{
    if (OnInit() == false)
    {
        data::log("Initialization failure.");
        return 1;
    }
    SDL_Event Event;
    while(running == true)
    {
        fps.start();
        while(SDL_PollEvent(&Event))
        {
            OnEvent(&Event);
        }
        OnRender();
        OnLoop();
        log(convertInt(1000/fps.get_ticks()));
    }
    OnCleanup();
    data::log("Program exited successfully.");
    return 0;
}

data.h:

#ifndef _DATA_H_
    #define _DATA_H_

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
#include <fstream>
#include <sstream>
#include "globals.h"

class timer
{
private:
    int     startTicks; //Clock time at timer start
    bool    started;    //Timer status
    int     frame;

public:
    void    Timer();  //Set timer variables

    //Clock actions
    void    start();
    void    stop();

    //Get timer status
    int     get_ticks();

    bool    is_started();   //Checks timer status
    void    incframe();
};

class data
{
private:
    timer fps;
    bool    running;
    SDL_Surface*    display;
    SDL_Surface*    tileset;    //The tileset
    SDL_Rect    clip[255];
    int     spritewidth;
    int     spriteheight;
    bool    mapfloor[80][24];     //Contains the locations of the floor and walls
    int    mapplayer[80][24];    //Contains the location of the player
    SDL_Rect   playersprite;       //Clip value of the sprite that represents the player
    std::string  tilesetdsk;    //Location of the tileset on disk
    std::string     debugfile;
    int     FRAMES_PER_SECOND;  //Max FPS

public:
    data();
    bool    OnExecute();
    bool    OnInit();
    void    OnEvent(SDL_Event* Event);
    void    OnLoop();
    void    OnRender();
    void    OnCleanup();
    static  SDL_Surface*    load_image(std::string filename);
    static  void    apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip);
    void    LoadSprite();   //Determines what sprite is where in the tileset
    bool    levelgen();     //Generates a level
    void    log(std::string);          //**DEBUG** function for logging to file
    void    setvars();
    std::string convertInt(int number);
};
#endif

OnLoop.cpp:

#include "data.h"
void data::OnLoop()
{
    if(fps.get_ticks() < 1000/FRAMES_PER_SECOND)
    {
        SDL_Delay((1000/FRAMES_PER_SECOND) - fps.get_ticks());
    }
    fps.incframe();
}


fps is a variable of type timer, declared in data::OnExecute().

However, you also reference fps in another method data::OnLoop(), where fps is out of scope.

To fix this, I recommend making fps a member-variable of class data. Then fps will always be available inside all the methods of data.

Do this by declaring fps right with your other member-variables
(in the private: section of class data)
Remove the declaration in data::OnExecute().


You must move the fps variable declaration to the header file. The fps variable is only defined in the data::OnExecute function scope. If you move it to be a class member, all the methods in the class can access it.

So:
1. Remove the "timer fps;" line from data::OnExecute.
2. Add "timer fps;" below the "private:" in data.h

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜