(非常)简单的 C++ 中的编译器问题从文件获取数字输入的程序
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int FILENAME_MAX=20;
int main() {
ifstream input;
char name[FILENAME_MAX + 1];
int value;
do {
cout << "Enter the filename (maximum of " << (FILENAME_MAX+1)
<< " characters: ";
cin >> name;
input.open(name);
} while(input.fail() );
while(input >> value) {
int count=1;
cout << "value #" << count << "\t" << value << endl;
count++;
}
return 0;
}
这是一段非常简单的代码,用于从文件中读取一些数字。 不幸的是我无法编译它。行后/行上有错误 “常量 int FILENAME_MAX=20;” 该错误显示“在数字常量之前预期有不合格的 id”。
有人可以告诉我我做错了什么吗?
我正在 Mac OS 10.5.8 上使用 Xcode 3.0 进行编译
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int FILENAME_MAX=20;
int main() {
ifstream input;
char name[FILENAME_MAX + 1];
int value;
do {
cout << "Enter the filename (maximum of " << (FILENAME_MAX+1)
<< " characters: ";
cin >> name;
input.open(name);
} while(input.fail() );
while(input >> value) {
int count=1;
cout << "value #" << count << "\t" << value << endl;
count++;
}
return 0;
}
This is a very simple piece of code for reading some numbers from a file.
Unfortunately I can't get it to compile. There is an error after/on the line
"const int FILENAME_MAX=20;"
The error says "expected unqualified-id before numeric constant."
Could someone please tell me what I am doing wrong?
I am compiling on Mac OS 10.5.8 with Xcode 3.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
FILENAME_MAX
是一个由标准库*,因此它已被用作标识符。当您尝试将其用作标识符时,它实际上在预处理过程中被替换为某个数字。数字不是有效的标识符,因此您会收到错误消息。 (这就是为什么它说“我期待一个标识符,而不是数字常量。”)将其重命名为其他名称。 (或者使用
std::string
,尽管您似乎还没有完全做到这一点。)*它是由
定义的。虽然您不直接包含它,但其他标准库标头可以自由地包含它们认为合适的任何其他标准标头。FILENAME_MAX
is a macro that is defined by the standard library*, and so it is already taken for use as an identifier. When you try to use it as an identifier, it's actually being replaced during preprocessing to some number. A number is not a valid identifier, so you get an error. (Which is why it's saying "I was expecting an identifier, not a numeric constant.")Rename it to something else. (Or use
std::string
, though it seems you aren't quite there yet.)*It is defined by
<cstdio>
. While you don't include it directly, other standard library headers are free to include any other standard headers as they see fit.为什么
FILENAME_MAX
全部大写?所有大写字母通常用于宏,当你点击一个(正如你所做的那样)时,预处理器会无意识地践踏你的代码,进行最愚蠢的替换。为宏保留这样的标识符,除非确实必须使用宏,否则不要使用宏(在 C++ 中很少出现这种情况),并且这种情况不会发生。
Why do you make
FILENAME_MAX
all upper-case? All upper-case is usually used for macros, and when you hit one (as you do) the preprocessor will mindlessly trample over your code doing the dumbest replacements.Reserve such identifiers for macros, don't use macros unless you really must (which is rarely ever the case in C++), and this won't happen.
FILENAME_MAX
可能是某个地方的#define
。尝试#undef FILENAME_MAX
。FILENAME_MAX
is probably a#define
somewhere. Try#undef FILENAME_MAX
.FILENAME_MAX 已在 stdio.h 中定义。
更改你的常量名称。
FILENAME_MAX is already defined in stdio.h.
Change your const name.
我在 Ubuntu Linux 上,我可以编译它。我将 FILENAME_MAX 更改为 F_M 并且成功了。
I am on Ubuntu Linux and I could compile it. I changed the FILENAME_MAX to F_M and it worked.