C++错误:预期')'在学习 SDL 的过程中
经过搜索,我觉得类似的问题已经被问过很多次了。尽管如此,我找不到答案,所以这里是:
我的设置: CodeBlocks、GCC C++ 和 SDL
我目前正在浏览 LazyFoo 的 SDL 教程,并收到此错误:
错误:“:”标记之前应有“)”
错误:此代码中的
:SDL_Surface* load_image(std::string myfile)
在此上下文中
#include“SDL/SDL.h”
#include <字符串.h>
常量 int SCREEN_WIDTH = 425;
常量 int SCREEN_HEIGHT = 550;
常量 int SCREEN_BPP = 32;SDL_Surface* 消息 = NULL;
SDL_Surface* 背景 = NULL;
SDL_Surface* 屏幕 = NULL;SDL_Surface* load_image(std::string myfile)
{
更多内容和程序的其余部分等
}
这可能与我在 CodeBlocks 中设置字符串或 SDL 库的方式有关。我错过了任何建议或明显的解决方案链接吗?
Given the searches, I feel as though this sort of thing has been asked many times. Nevertheless, I couldn't find an answer so here goes:
My setup:
CodeBlocks, GCC C++ and SDL
I'm currently going through LazyFoo's SDL tutorials, and am getting this error:
error: expected ')' before ':' token
from this code:
SDL_Surface* load_image(std::string myfile)
in this context
#include "SDL/SDL.h"
#include < string.h >
const int SCREEN_WIDTH = 425;
const int SCREEN_HEIGHT = 550;
const int SCREEN_BPP = 32;SDL_Surface* message = NULL;
SDL_Surface* background = NULL;
SDL_Surface* screen = NULL;SDL_Surface* load_image(std::string myfile)
{
more stuff and the rest of the program etc
}
I feel that this may have something to do with the way my string or SDL libraries are set up in CodeBlocks. Any suggestions or obvious links to solutions that I missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
包含
而不是< string.h >
如果您想使用 C++ STL 字符串库。Include
<string>
instead of< string.h >
if you want to use the C++ STL strings library.您应该在标头的包含内容中使用
string
而不是string.h
。You should use
string
instead ofstring.h
in the inclusion from the header.我还在关注以下教程: http://lazyfoo.net/SDL_tutorials/lesson02/index.php
并收到消息:
第 3 行,错误:字符串:没有这样的文件或目录
第 15 行,错误:在 ':' 标记之前预期有 ')'
我想我找到了问题的原因。
当选择“文件/新建/空文件”时,它默认为文件扩展名 Untitled1.c ,我将文件扩展名更改为 .cpp ,这使得代码块识别 #include ,现在应用程序可以运行。
I was also following the tutorial at: http://lazyfoo.net/SDL_tutorials/lesson02/index.php
and got the message:
line 3, error: string: No such file or directory
line 15, error: expected ')' before ':' token
i think i found the reason for the problem.
When choosing "File / New / Empty file", it defaults to the file extension Untitled1.c , i changed the file extension to .cpp and that made Code Blocks recognize #include , and now the application works.
这听起来像是一个 C 错误;您应该使用
g++
进行编译,而不是gcc
。That sounds like a C error; you should be compiling with
g++
, notgcc
.我想通了。我遇到了同样的问题,但我成功了。这正是我所做的。首先下载了lazy foo在教程最后给出的源文件。我尝试了它们,但收到错误消息:在计算机上找不到 SDL.dll。我将 SDL.dll 文件从项目所在的文件夹(本来应该可以正常工作)移至 C:/windows/system32。
接下来,我再次尝试了该程序,即教程附带的程序,窗口闪烁但没有任何显示,我将两个 bpm 文件复制到项目中并且它起作用了。
当然,该文件必须是 .cpp,所以我会这么说。
等等还有更多。现在我确信我在第二课中的尝试没有错误,我完全按照我看到的那样输入了所有内容。但这就是问题所在。
loadImage = SDL_LoadBMP( 文件名.c_str() ); <__在他展示这一点的地方,使用他使用的字体,它看起来更像是loadedImage = SDL_LoadBMP( filename.c_str0 );
看看它……确实如此,他输入 () 的地方看起来像 0,你真的必须知道。
因此,任何遇到此问题的人,以及您得到的任何其他建议,我建议您检查代码中是否存在您可能已阅读并输入为 0 的棘手 ()。
I figured it out. I had the same problem but I got it working. Here is exactly what I did. First downloaded the sourcefiles that lazy foo gave at the end of the tutorial. I tried them and I got the error that SDL.dll was not found on computer. I moved the SDL.dll file from the folder the project was in which should have worked fine, to the C:/windows/system32.
Next, I tried the program again, the one that comes with the tutorial, the window flashed but with nothing showing, I copied in the two bpm files in to the project and it worked.
Definitely the file needs to be .cpp so I'll say that.
Hold on there's more. Now I thought for sure that my attempt at lesson2 had no errors, I typed everything exactly as I saw it. However that was the problem.
loadedImage = SDL_LoadBMP( filename.c_str() ); <__where he shows this, with the font he uses, it looks more like loadedImage = SDL_LoadBMP( filename.c_str0 );
Just look at it...it does, anwhere he types () will look like a 0 and you really have to know.
So anyone having this problem, as well as any other advice you get I suggest checking your code for the tricky () that you might have read and typed in as a 0.