简体   繁体   中英

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. I'm pretty sure it has something to do with endl; because when I remove '<< 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; 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. Remove this semicolon (you have also this problem in your function). Also, you have number 1 instead of l in endl . 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:

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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