我只是想让自己熟悉从Java迁移过来的C++的基础知识。我刚刚写了这个功能禁用程序,遇到了一个错误的test.cpp:15: error: expected primary-expression before ‘<<’ token,我不确定为什么。
有人愿意解释一下为什么endl不使用常量吗?代码如下。
//Includes to provide functionality.
#include <iostream>
//Uses the standard namespace.
using namespace std;
//Define constants.
#define STRING "C++ is working on this machine usig the GCC/G++ compiler";
//Main function.
int main()
{
string enteredString;
cout << STRING << endl;
cout << "Please enter a String:" << endl;
cin >> enteredString;
cout << "Your String was:" << endl;
cout << enteredString << endl;
return(0);
}发布于 2011-07-12 04:43:14
您的预处理器定义中有一个;。请注意,#DEFINE STRING x只是将整个x语句(包括;)复制到它被引用的位置。
此外,预处理器常量不是语言常量。您应该使用const string STRING("C++ is working on this machine usig the GCC/G++ compiler");
发布于 2011-07-12 04:42:29
您的#define在末尾有一个分号。这将成为宏的一部分,因此预处理代码如下所示:
cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;去掉分号,你就没问题了。
PS:这通常是一个更好的想法,使用实数常量而不是依赖预处理器:
const char *STRING = "C++ is working on this machine usig the GCC/G++ compiler";发布于 2011-07-12 04:43:10
你的#define的末尾有一个分号--这将被替换到你的代码中,给出。
cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;
https://stackoverflow.com/questions/6656246
复制相似问题