2011-10-02 111 views
1

編譯:MinGW的
IDE:代碼::塊
平臺:Windows XP中
外部庫:SDL,SDL_image
我想要做的事:在60蓋上幀率幀每秒使用一個定時器,每個循環後重置。
只是一個警告,我對C++沒有經驗,但我已經完成了研究,並且在其他任何地方都找不到這種錯誤。我相信它與範圍有關,但我不知道如何解決它。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; 
} 

噠ta.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(); 
} 
+0

請向我們展示每個文件中的#include文件嗎? –

回答

3

fpstimer類型的變量,在data::OnExecute()聲明。

但是,您還在另一種方法data::OnLoop()中參考fps,其中fps超出了範圍。

爲了解決這個問題,我建議使fpsdata類的成員變量。然後fps將始終在data的所有方法中可用。

通過聲明FPS權與其他成員變量
(在class dataprivate:部分)
data::OnExecute()取出聲明執行此操作。

+0

這樣做會返回C:\ Documents and Settings \ Evan \ My Documents \ Programming Projects \ Roguelike SDLtest 2 \ data.h | 26 | error:field'fps'has incomplete type | – ArcticVanguard

+0

顯示您的新代碼。 – abelenky

+0

@北極,你從哪裏得到計時器對象?它看起來像你忘了#include一個頭。 – Septagram

0

您必須將fps變量聲明移動到頭文件。 fps變量僅在data :: OnExecute函數作用域中定義。如果您將其移動爲類成員,則該類中的所有方法都可以訪問它。

所以:
1.刪除 「計時器FPS;」來自data :: OnExecute的行。
2.添加「timer fps;」在data.h中的「private:」下面。