0

我最近深入研究了一些 SDL 教程,但在编译这个教程时遇到了困难,特别是使用 DevC++ 和 SDL 库: http ://lazyfoo.net/SDL_tutorials/lesson05/index.php

我收到这个特殊的错误:`filename' undeclared (first use this function) and it points to the filename.c_str()); 代码区域,以及编译日志中列出的其他一些区域。我还想调查未被识别的“apply_surface”。我已包含以下标题:

#include <cmath>
#include <string>
#include <vector>
#include <iostream>
#include "SDL/SDL.h"
#include "quickcg.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"

using namespace QuickCG;

链接器选项包括基于我发现的具有相同问题的其他线程的建议,看来我并不孤单,但我还没有完全找到适用于我的代码的解决方案:

-lmingw32
-mwindows
-lSDLmain
-lSDL
-lSDL_mixer
-lSDL_image
-lSDL_ttf
-lstdc++

代码片段:

int main(int /*argc*/, char */*argv*/[])
{
    {
    SDL_Surface* title = NULL;
    SDL_Surface* hud = NULL;
    SDL_Surface* screen = NULL;
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load( filename.c_str() );

    if (loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );
        SDL_FreeSurface( loadedImage );
        if( optimizedImage != NULL)

等等等等

编译日志:

Compiler: Default compiler
Building Makefile: "D:\Coding\Raycaster\Makefile.win"
Executing  make...
make.exe -f "D:\Coding\Raycaster\Makefile.win" all
g++.exe -c raycaster.cpp -o raycaster.o -I"D:/Coding/Dev-Cpp/lib/gcc/mingw32/3.4.2    
/include"  -I"D:/Coding/Dev-Cpp/include/c++/3.4.2/backward"  -I"D:/Coding/Dev-Cpp
/include/c++/3.4.2/mingw32"  -I"D:/Coding/Dev-Cpp/include/c++/3.4.2"  -I"D:/Coding
/Dev-Cpp/include"    -fexpensive-optimizations -O3 -mwindows

raycaster.cpp: In function `int SDL_main(int, char**)':
raycaster.cpp:117: error: `filename' undeclared (first use this function)
raycaster.cpp:117: error: (Each undeclared identifier is reported only once for
each function it appears in.)

raycaster.cpp:130: error: invalid conversion from `SDL_Surface*' to `int'
raycaster.cpp:133: error: `apply_surface' undeclared (first use this function)
raycaster.cpp:134: error: `hud' undeclared (first use this function)

make.exe: *** [raycaster.o] Error 1

Execution terminated

任何建议或意见,将不胜感激。我是整个 C++ 场景的又一个笨蛋新手,但这更像是一个学习实验,而不是其他任何东西。我只是喜欢构建一个基本的光线投射器,并且一直坚持让 hud 显示。

4

1 回答 1

0

第一:你为什么注释掉main函数的一部分int main(int /*argc*/, char */*argv*/[])???它应该是这样的:int main(int argc, char *argv[])

第二:除非您filename在 main 之前在全局范围内声明了变量(错误表明您没有),否则您需要声明它并为其提供一个值。由于您使用的.c_str()方法filename可能应该是str::string. 尝试在 IMG_Load 行之前添加:

std::string filename = "path to your file here";

如果您想知道其余代码有什么问题,您也必须发布它。

于 2014-04-01T10:14:36.310 回答