首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有常量的C++结尾

带有常量的C++结尾
EN

Stack Overflow用户
提问于 2011-07-12 04:39:37
回答 8查看 391关注 0票数 0

我只是想让自己熟悉从Java迁移过来的C++的基础知识。我刚刚写了这个功能禁用程序,遇到了一个错误的test.cpp:15: error: expected primary-expression before ‘<<’ token,我不确定为什么。

有人愿意解释一下为什么endl不使用常量吗?代码如下。

代码语言:javascript
复制
//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);
}
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2011-07-12 04:43:14

您的预处理器定义中有一个;。请注意,#DEFINE STRING x只是将整个x语句(包括;)复制到它被引用的位置。

此外,预处理器常量不是语言常量。您应该使用const string STRING("C++ is working on this machine usig the GCC/G++ compiler");

票数 6
EN

Stack Overflow用户

发布于 2011-07-12 04:42:29

您的#define在末尾有一个分号。这将成为宏的一部分,因此预处理代码如下所示:

代码语言:javascript
复制
cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;

去掉分号,你就没问题了。

PS:这通常是一个更好的想法,使用实数常量而不是依赖预处理器:

代码语言:javascript
复制
const char *STRING = "C++ is working on this machine usig the GCC/G++ compiler";
票数 8
EN

Stack Overflow用户

发布于 2011-07-12 04:43:10

你的#define的末尾有一个分号--这将被替换到你的代码中,给出。

cout << "C++ is working on this machine usig the GCC/G++ compiler"; << endl;

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6656246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档