c++ - 错误 : expected primary-expression before 'int'

标签 c++

我是 C++ 的初学者。我正在读一本关于 C++ 的书。我用g++编译了下面的程序,这是书中的例子:

/*modified fig1-1.cpp*/
#include <iostream>
using namespace std;
int main()
{
    cout << "\n Enter an integer";
    cin >> (int i);
    cout << "\n Enter a character";
    cin >> (char c);
    return 0;
}

然后我收到以下错误消息:

fig1-2.cpp: In function 'int main()':
fig1-2.cpp:7:10: error: expected primary-expression before 'int'
  cin >> (int i);
          ^
fig1-2.cpp:7:10: error: expected ')' before 'int'
fig1-2.cpp:9:10: error: expected primary-expression before 'char'
  cin >> (char c);
          ^
fig1-2.cpp:9:10: error: expected ')' before 'char'

谁能告诉我发生了什么事?非常感谢您。

最佳答案

int i 是声明的语法。它可能不会出现在表达式中,表达式应该跟在 cin >> 之后。

首先声明你的变量然后使用它:

int i;
cin >> i;

char c 也是如此:

chat c;
cin >> c;

而且我非常怀疑这是一本 C++ 教材中的示例。这是明显错误的语法。如果它确实在书中作为一个假定的工作示例(即不解释错误),那么您应该得到另一本书。

关于c++ - 错误 : expected primary-expression before 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39909051/

相关文章:

c++ - 使用Go + SWIG + C++代码清理内存管理

c++ - 将交换与对象自杀一起使用

C++11:使用正则表达式进行两种可能的匹配次数的安全实践

C++ Builder nullptr 关键字 "Undefined"

c++ - 常量函数由非常量对象调用。为什么?

c++ - 使用 Texture2DArray 进行批处理

c++ - 如何将此指针分配给我的 Iterator 类

c++ - Qt。集成 C++ 和 Qml

c++ - 提供可变参数模板 : candidate expects 1 argument, 0(推导错误)

c++ - 循环列表遍历引擎