【问题标题】:C++ - Undefined reference to an objectC ++ - 对对象的未定义引用
【发布时间】:2011-10-02 19:49:41
【问题描述】:

编译器:MinGW
IDE:代码::块
平台:Windows XP
外部库:SDL、SDL_image
我正在尝试做的事情:使用在每个循环后重置的计时器将帧速率限制在每秒 60 帧。
只是一个警告,我对 C++ 缺乏经验,但我已经做过研究,在其他任何地方都找不到这种错误。我确定它与范围有关,但我不确定如何解决它。


编译器输出:
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();
}

【问题讨论】:

  • 请告诉我们每个文件中的#include 文件?

标签: c++ oop object scope


【解决方案1】:

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

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

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

通过使用其他成员变量声明 fps 来做到这一点
(在 class dataprivate: 部分中)
删除data::OnExecute()中的声明。

【讨论】:

  • 这样做会返回 C:\Documents and Settings\Evan\My Documents\Programming Projects\Roguelike SDLtest 2\data.h|26|错误:字段 'fps' 的类型不完整|
  • @Arctic,你从哪里得到计时器对象?您似乎忘记了#include 标题。
  • @Septagram,嗯?该对象在data.h中,我只是没有更新这里的代码。我的错。
  • 哦,是的,我的错。尝试添加“class timer;”行就在“数据”类定义之前声明“计时器”类。虽然如果没有声明,错误信息应该是不同的,所以我不保证它会有所帮助。抱歉,我困了,脑子有雾。
  • 已更新,同样的错误。不过,我可以与困倦联系起来,哈哈。
【解决方案2】:

您必须将 fps 变量声明移至头文件。 fps 变量仅在 data::OnExecute 函数范围内定义。如果将其移动为类成员,则类中的所有方法都可以访问它。

所以:
1.去掉“定时器fps;”数据行::OnExecute.
2.增加“定时器fps;”在 data.h 中的“private:”下方

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多