对对象的未定义引用
C++ - Undefined reference to an object
编译器: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();
}
fps
是timer
类型的变量,在data::OnExecute()
中声明。
但是,您还在另一个方法data::OnLoop()
中引用fps
,其中fps
不在范围内。
为了解决这个问题,我建议将fps
作为data
类的成员变量。那么fps
将始终在data
的所有方法中可用。
通过在其他成员变量
中声明fps(在class data
的private:
部分)
删除data::OnExecute()
中的声明
必须将fps变量声明移到头文件中。fps变量只在data::OnExecute函数作用域中定义。如果将其移动为类成员,则类中的所有方法都可以访问它。
:
1. 删除data::OnExecute中的"timer fps;"行。
2. 在data.h
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中