当添加此statment(the_pointer的类型是int *的)C++ - 错误:前预期主表达式 '<<' 令牌
<<"\nThe contents of the variable the_pointer is pointing at is : "<<*the_pointer;
编译器返回以下错误:
error: expected primary-expression before '<<' token
这是为什么?
谢谢。
当添加此statment(the_pointer的类型是int *的)C++ - 错误:前预期主表达式 '<<' 令牌
<<"\nThe contents of the variable the_pointer is pointing at is : "<<*the_pointer;
编译器返回以下错误:
error: expected primary-expression before '<<' token
这是为什么?
谢谢。
<<
是一个运算符,它有两个参数 - 左手和右手。你只提供了右手边。你的代码更改为:
std::cout << "\nThe contents of the variable the_pointer is pointing at is : " << *the_pointer;
并确保您#include <iostream>
附近的源文件的顶部,这样就可以使用std::cout
。
因为<<
不是一元前缀运算符,所以需要两个操作数。当用于流输出时,左边的操作数是一个输出流,右边的操作数是你想要发送给流的内容。结果是对同一个流的引用,因此您可以在其中添加更多<<
子句。但无论如何,您始终需要左操作数。
下面的程序编译和运行良好:
#include <iostream>
int main(int argc, char *argv[]) {
int val = 10;
int *ptr_val = &val;
std::cout << "pointer value: \n";
std::cout << *ptr_val;
return 0;
}
通过您的问题您的评论来看,你有这样的事情:
std::cout << x
<< y
<< z ;
这都是一个说法,因为没有x或y之后的分号结尾语句。但是下一个这样的行会失败,因为z之后的分号。
你需要使用`std :: cout << ...`。仅使用<< <<是语法错误。 – 6502 2011-02-13 11:22:11