对对象的未定义引用

C++ - Undefined reference to an object

本文关键字:引用 未定义 对象      更新时间:2023-10-16

编译器:MinGW
IDE:代码::块
平台:Windows XP
外部库:SDL, SDL_image
我想做的是:使用计时器在每次循环后重置,将帧率限制在每秒60帧。
只是一个警告,我对c++没有经验,但我做了研究,我在其他地方找不到这种错误。我确信这与作用域有关,但我不确定如何解决它。

编译器输出:



In function ZN4data6OnLoopEv':|'C:Documents and SettingsEvanMy DocumentsProgramming ProjectsRoguelike SDLtest 2OnLoop.cpp|5|undefined reference to 'fps'

C:Documents and SettingsEvanMy DocumentsProgramming ProjectsRoguelike SDLtest 2OnLoop.cpp|7|undefined reference to 'fps'

C:Documents and SettingsEvanMy DocumentsProgramming ProjectsRoguelike SDLtest 2OnLoop.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();
}

fpstimer类型的变量,在data::OnExecute()中声明。

但是,您还在另一个方法data::OnLoop()中引用fps,其中fps不在范围内。

为了解决这个问题,我建议将fps作为data类的成员变量。那么fps将始终在data的所有方法中可用。

通过在其他成员变量
中声明fps(在class dataprivate:部分)
删除data::OnExecute()中的声明

必须将fps变量声明移到头文件中。fps变量只在data::OnExecute函数作用域中定义。如果将其移动为类成员,则类中的所有方法都可以访问它。

:
1. 删除data::OnExecute中的"timer fps;"行。
2. 在data.h

中的"private:"下面添加"timer fps;"