简体   繁体   English

我为什么会收到“错误:'<<'标记之前的预期主表达式。”

[英]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.

相关问题 为什么我会收到此错误? “&#39;;&#39;之前的预期主要表达式令牌” - Why am I getting this error? "expected primary-expression before ‘;’ token" 我的代码看起来不错。 为什么会收到“错误:&#39;)&#39;标记之前的预期主表达式”? - My code looks fine. Why am I getting a “error: expected primary-expression before ')' token”? 为什么我在这段代码中收到错误? 错误是:“long”之前的预期主表达式 - Why I am getting error in this code?? And the error is: expected primary-expression before ‘long’ 为什么我会收到此错误:“bool”之前的预期主表达式? - Why am I getting this error-:expected primary-expression before 'bool'? 为什么在&#39;&#39;c ++之前会出现[Error]预期的主表达式? - Why am I getting [Error] expected primary-expression before ' ' c++? 错误:“]”标记之前的预期主表达式。 需要帮助 function 定义和调用 - Error: expected primary-expression before ']' token. Need assitance with function definition and calling 出现错误 - ']' 标记之前的预期主表达式 - Getting Error - expected primary-expression before ‘]’ token 得到“错误:']'标记之前的预期主表达式”c++ - Getting "error: expected primary-expression before ']' token" c++ 错误:“(”令牌之前的预期主表达式 - error: expected primary-expression before ‘(’ token 错误:“ {”令牌之前的预期主表达式 - error: expected primary-expression before '{' token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM