C++ 编译错误

标签 c++ compiler-errors token

我无法弄清楚为什么在尝试编译时会出现这些错误。我从未遇到过“expected _ before _ token”错误,但我相信它们很常见(如果有问题请随时启发我)。

pe4.cpp: In function 'int main()':
pe4.cpp:18: error: expected ')' before ';' token
pe4.cpp:18: error: expected ';' before ')' token
pe4.cpp:45: error: a function-definition is not allowed here before '{' token
pe4.cpp:51: error: a function-definition is not allowed here before '{' token
pe4.cpp:57: error: a function-definition is not allowed here before '{' token

#include <iostream>

using namespace std;

void printStar(int);
void printSpace(int);
void printNewLine();

int main()
{
    int side, i, j;

    if (i=0; i < 2; i++)
    {
        cout << "Enter side: " << endl;
        cin << side;

        if (side < 3 || side > 20)
        {
            cout << "Out of Bounds!!!"
            return 0;
        }

        printStar(side);
        printNewLine();

        {
            printStar(1);
            printSpace(side-2);
            printStar(1);
            printNewLine();
        }

        printStar(side);
        printNewLine();
    }

    void printStar(int a)
    {
        for (int j = 0; j < a; j++)
            cout << "*";
    }

    void printSpace(int a)
    {
        for (int j = 0; j < a; j++)
            cout << " ";
    }

    void printNewLine()
    {
        cout << endl;
    }
}

最佳答案

你没有;cout << "Out of Bounds!!!" 的末尾行。

你有 if (i=0; i < 2; i++) ;那应该是 for (i=0;... .

你有 cin << side ;那应该是 cin >> side .

您已经在内部定义了您的函数体 main() ;他们应该住在外面。

关于C++ 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5849639/

相关文章:

c++ - OpenCV 等角旋转

c++ - 编译十六进制网格程序时错误代码C2768

iphone - 验证 iPhone 推送通知 token ?

c++ - clang 或 gcc 关于此内部类成员访问是否正确?

c++ - 具有来自同一类的回调的 Poco 计时器

c++ - OpenSSL 在 x86 上编译失败,认为在 x86_64 上编译成功

c++ - MinGW pacman -Sys 无法锁定数据库

c++ - 使用 CMake 编译带有链接 dll 的应用程序。需要查看工作示例

c# - WebAPI - 如何从 token 中获取 UserID

security - RSA JWT key 轮换周期?