[英]Why am I getting “Error: expected primary-expression before '<<' token.”
I keep getting an error and I don't know what I'm missing. 我不断收到错误消息,但我不知道自己缺少什么。 This is only my first programming class, so it could be something really simple that I'm not seeing. 这只是我的第一门编程课,所以可能是我没看到的非常简单的事情。 Help? 救命? :) :)
#include <iostream>
using namespace std;
const float METERS_PER_INCH = 39.3700787;
const float METERS_PER_FOOT = 3.2808399;
const float METERS_PER_YARD = 1.09361;
int main ()
{
int yards;
int feet;
int inches;
int totalMeters;
cout << "Enter a length in meters: ";
cin >> totalMeters;
cout << endl;
cout << "The total length is " << endl;
<< totalMeters * METERS_PER_YARD << "yards;"
<< yards * METERS_PER_FOOT << "feet;"
<< feet * METERS_PER_INCH << "inches;"
<< endl;
return 0;
}
Change your last cout
to: 改变你的最后cout
到:
cout << "The total length is " << endl
<< totalMeters * METERS_PER_YARD << "yards;"
<< yards * METERS_PER_FOOT << "feet;"
<< feet * METERS_PER_INCH << "inches;"
<< endl;
The difference is the ;
区别在于;
removed after the first endl
. 在第一个endl
之后删除。
With the semicolon there, the first line of the cout
is a statement on its own, so the compiler looks for a new statement on the next line. 使用分号时, cout
的第一行本身就是一条语句,因此编译器在下一行寻找新的语句。 Statements cannot just start with <<
because it's a binary operator - it expects an expression on its left-hand-side as well. 语句不能仅以<<
开头,因为它是一个二进制运算符-它也希望左侧有一个表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.