简体   繁体   English

C ++预期在'<<'Endl之前的主表达式

[英]C++ Expected Primary Expression before '<<' Endl

I keep getting an expected primary-expression before '<<' token compile error message when I try to compile the following code. 当我尝试编译以下代码时, expected primary-expression before '<<' token编译错误消息expected primary-expression before '<<' token我一直得到expected primary-expression before '<<' token I'm pretty sure it has something to do with endl; 我很确定这与endl有关; because when I remove '<< endl; 因为当我删除'<< endl; ' from each part of the code it works fine 从代码的每个部分都能正常工作

#include <cstdlib>
#include <iostream>

using namespace std;
int num1 = 0;

int funcNum1()
{
    cout << num1; << endl;
    int num1 = 2;
    cout << num1; << endl;
    return 0;
}

int main(int argc, char *argv[])
{
    cout << num1; << end1;
    int num1 = 1;
    funcNum1();
    cout << num1; << end1;
    system("PAUSE");
    return 0;
}

You have made mistake in line cout << num1; << end1; 您在cout << num1; << end1;行中犯了错误cout << num1; << end1; cout << num1; << end1; change it to 更改为

cout << num1 << endl;

Chance this at both the places in your code. 在代码中的两个位置都可以这样做。

As you can see from this code, after the num1 you have semicolon. 从该代码中可以看到,在num1您有分号。 Remove this semicolon (you have also this problem in your function). 删除该分号(您的函数中也存在此问题)。 Also, you have number 1 instead of l in endl . 另外,您在endl使用数字1代替l。 Fix this two error and it should work. 修复这两个错误,它应该可以工作。

int main(int argc, char *argv[])
{
    cout << num1; << end1;
    int num1 = 1;
    funcNum1();
    cout << num1; << end1;
    system("PAUSE");
    return 0;
}

In main() your syntax is off: 在main()中,语法关闭:

    int main(int argc, char *argv[]) {

    cout << num1 << endl; // watch the semicolons, use "endl" not "end1"
    int num1 = 1;
    funcNum1();
    cout << num1 << endl; // watch the semicolons, use "endl" not "end1"
    system("PAUSE"); // system needs #include <stdlib.h>
    return 0;
}

You need the "stdlib.h" which is embedded in the "cstdlib" header to access the system() method: take a look at the documentation 您需要嵌入在“ cstdlib”标头中的“ stdlib.h”来访问system()方法:看一下文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM