[英]C++ - error: expected primary-expression before '<<' token
When I add this statment (the_pointer is of type int*) 当我添加此语句时(the_pointer的类型为int *)
<<"\nThe contents of the variable the_pointer is pointing at is : "<<*the_pointer;
The compiler returns the following error: 编译器返回以下错误:
error: expected primary-expression before '<<' token
Why is that? 这是为什么?
Thanks. 谢谢。
Judging by your comment to your question, you had something like this: 通过对问题的评论来判断,您有类似以下内容:
std::cout << x
<< y
<< z ;
This is all one statement, because there is no semi-colon statement terminator after x or y. 这是全部一个语句,因为在x或y之后没有分号语句终止符。 But the next such line will fail, because of the semi-colon after z.
但是下一个这样的行将失败,因为z后面的分号。
<<
is an operator which takes two arguments - left-hand and right-hand. <<
是带有两个参数的运算符-左手和右手。 You have only provided the right-hand side. 您只提供了右侧。 Change your code to:
将您的代码更改为:
std::cout << "\nThe contents of the variable the_pointer is pointing at is : " << *the_pointer;
And make sure you #include <iostream>
near the top of your source file so you can use std::cout
. 并确保在源文件顶部附近
#include <iostream>
以便可以使用std::cout
。
Because <<
is not a unary prefix operator, it needs two operands. 因为
<<
不是一元前缀运算符,所以它需要两个操作数。 When used for stream output, the left operand is an output stream, and the right operand is what you want to send to the stream. 用于流输出时,左操作数是输出流,右操作数是要发送到该流的内容。 The result is a reference to the same stream, so you can append more
<<
clauses to it. 结果是对同一个流的引用,因此您可以向其附加更多
<<
子句。 In any case, however, you need a left operand at all times. 但是,无论如何,您始终都需要一个左操作数。
The following program compiles and runs fine: 以下程序可以编译并正常运行:
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.