error: ISO C++ forbids declaration of `FILE' with no type

本文详细介绍了在使用g++编译cpp源文件时遇到的错误,该错误源于源文件中引用的头文件未包含stdio.h。通过在引用头文件前加入#include<stdio.h>,成功解决了编译问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用g++编译一个cpp的源文件时报错如题。源文件中引用了另一个头文件,那个头文件中声明了一个FILE的指针变量,但没有包含stdio.h,源文件对应的头文件也没有显式包含。

在引用的那个头文件之前加上#include stdio.h>

编译通过。

g++ snake.cpp -o snake -lncurses snake.cpp:7:0: warning: "KEY_UP" redefined [enabled by default] #define KEY_UP 65 ^ In file included from snake.cpp:1:0: /usr/include/curses.h:1410:0: note: this is the location of the previous definition #define KEY_UP 0403 /* up-arrow key */ ^ snake.cpp:8:0: warning: "KEY_DOWN" redefined [enabled by default] #define KEY_DOWN 66 ^ In file included from snake.cpp:1:0: /usr/include/curses.h:1409:0: note: this is the location of the previous definition #define KEY_DOWN 0402 /* down-arrow key */ ^ snake.cpp:9:0: warning: "KEY_RIGHT" redefined [enabled by default] #define KEY_RIGHT 67 ^ In file included from snake.cpp:1:0: /usr/include/curses.h:1412:0: note: this is the location of the previous definition #define KEY_RIGHT 0405 /* right-arrow key */ ^ snake.cpp:10:0: warning: "KEY_LEFT" redefined [enabled by default] #define KEY_LEFT 68 ^ In file included from snake.cpp:1:0: /usr/include/curses.h:1411:0: note: this is the location of the previous definition #define KEY_LEFT 0404 /* left-arrow key */ ^ snake.cpp: In member function ‘void SnakeGame::generate_food()’: snake.cpp:25:23: error: ISO C++ forbids declaration of ‘p’ with no type [-fpermissive] for(auto& p : snake) ^ snake.cpp:25:27: error: range-based ‘for’ loops are not allowed in C++98 mode for(auto& p : snake) ^ snake.cpp:26:22: error: request for member ‘x’ in ‘p’, which is of non-class type ‘int’ if(p.x == food.x && p.y == food.y) continue; ^ snake.cpp:26:39: error: request for member ‘y’ in ‘p’, which is of non-class type ‘int’ if(p.x == food.x && p.y == food.y) continue; ^ snake.cpp: In constructor ‘SnakeGame::SnakeGame()’: snake.cpp:33:20: error: ‘nullptr’ was not declared in this scope srand(time(nullptr)); ^ snake.cpp:34:24: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default] snake.push_back({LINES/2, COLS/2}); ^ snake.cpp:34:42: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default] snake.push_back({LINES/2, COLS/2}); ^ snake.cpp: In member function ‘void SnakeGame::update()’: snake.cpp:69:19: error: ISO C++ forbids declaration of ‘p’ with no type [-fpermissive] for(auto& p : snake) ^ snake.cpp:69:23: error: range-based ‘for’ loops are not allowed in C++98 mode for(auto& p : snake) ^ snake.cpp:70:18: error: request for member ‘x’ in ‘p’, which is of non-class type ‘int’ if(p.x == head.x && p.y == head.y) { ^ snake.cpp:70:35: error: request for member ‘y’ in ‘p’, which is of non-class type ‘int’ if(p.x == head.x && p.y == head.y) { ^ snake.cpp: In member function ‘void SnakeGame::draw()’: snake.cpp:93:19: error: ISO C++ forbids declaration of ‘p’ with no type [-fpermissive] for(auto& p : snake) ^ snake.cpp:93:23: error: range-based ‘for’ loops are not allowed in C++98 mode for(auto& p : snake) ^ In file included from snake.cpp:1:0: snake.cpp:94:23: error: request for member ‘x’ in ‘p’, which is of non-class type ‘int’ mvaddch(p.x, p.y, '*'); ^ snake.cpp:94:28: error: request for member ‘y’ in ‘p’, which is of non-class type ‘int’ mvaddch(p.x, p.y, '*'); ^ snake.cpp: In function ‘int main()’: snake.cpp:124:49: error: ‘class SnakeGame’ has no member named ‘get_score’ printf("Game Over! Final Score: %d\n", game.get_score()); ^ [root@localhost ~]#
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值