简体   繁体   English

问题是 C++ 中的“错误:需要分号”

[英]The problem is 'Error: semicolon is required' in c++

using namespace std;
#define MAX_STACK_SIZE 50

class Stack {
public:
    int top = -1;
    Location data[MAX_STACK_SIZE];
    bool isEmpty() { return top == -1; }
    bool isFull() { return top == MAX_STACK_SIZE - 1; }
    void push(Location& e) {
        if (!isFull()) {
            data[++top] = e;
        }
    }
    Location& pop() {
        if (!isEmpty()) {
            return data[top--];
        }
    }
    Location& peek() {
        return data[top];
    }
};

struct Location {
    int row;
    int col;
    Location(int r,int c): row(r), col(c){}
};

int main()
{
    const int MAX_SIZE = 6;
    char map[MAX_SIZE][MAX_SIZE] = {
        {'1','1','1','1','1','1'},
        {'e','0','1','0','0','1'},
        {'1','0','0','0','1','1'},
        {'1','0','1','0','1','1'},
        {'1','0','1','0','0','x'},
        {'1','1','1','1','1','1'},
    };

    bool isValid(int r, int c) {
        if (r < 0 || c < 0 || r >= MAX_SIZE || c >= MAX_SIZE) return false;
        else  return map[r][c] = '0' || map[r][c] = 'x';
    }
}

I am trying to implement maze navigation as a stack.我正在尝试将迷宫导航实现为堆栈。 The isValid function says i need ';' isValid 函数说我需要';' Marking the potential fixes ' bool isValid(int r,int c);标记潜在的修复 ' bool isValid(int r,int c); ' They suggest me use it like this. '他们建议我像这样使用它。 What is the problem?问题是什么? Please help me TT请帮帮我TT

It is not allowed in C++ to have regular functions inside other functions.在 C++ 中不允许在其他函数中包含常规函数。 You have to use closure/lambda function syntax.您必须使用闭包/lambda函数语法。

Ie rewrite string bool isValid(int r, int c) { to auto isValid = [&](int r, int c) -> bool { , plus add ;即重写字符串bool isValid(int r, int c) {auto isValid = [&](int r, int c) -> bool { ,加上 add ; at the end of such lambda function definition.在这样的 lambda 函数定义的末尾。

Then you can use isValid like regular function afterwards.然后你可以像使用常规函数一样使用isValid

Full code of main() function with needed fixes down below: main()函数的完整代码以及下面需要的修复:

Try it online! 在线试试吧!

int main()
{
    const int MAX_SIZE = 6;
    char map[MAX_SIZE][MAX_SIZE] = {
        {'1','1','1','1','1','1'},
        {'e','0','1','0','0','1'},
        {'1','0','0','0','1','1'},
        {'1','0','1','0','1','1'},
        {'1','0','1','0','0','x'},
        {'1','1','1','1','1','1'},
    };

    auto isValid = [&](int r, int c) -> bool {
        if (r < 0 || c < 0 || r >= MAX_SIZE || c >= MAX_SIZE) return false;
        else  return map[r][c] == '0' || map[r][c] == 'x';
    };

    bool try_is_valid = isValid(1, 2);

    return 0;
}

While Arty's answer works, it doesn't really address your confusion.虽然 Arty 的答案有效,但它并没有真正解决您的困惑。 The simple answer is: C++ doesn't allow nested function definitions.简单的答案是:C++ 不允许嵌套函数定义。 So you can't define function isValid inside function main .所以你不能在函数main定义函数isValid (Arty's answer shows you how to circumvent this restriction, but I would say it is rather too advanced for what you need.) (Arty 的回答向您展示了如何规避此限制,但我会说它对于您的需要来说太先进了。)

So you have to define the isValid function outside main .所以你必须在main之外定义isValid函数。 isValid won't be called from anywhere else, so it is good practice to declare it static so that it doesn't clash with other functions of the same name that may be defined in different source files. isValid不会从其他任何地方调用,因此将其声明为static是一种很好的做法,这样它就不会与可能在不同源文件中定义的其他同名函数发生冲突。 (And even if you only have one source file, it's still a good habit to get into.) (即使你只有一个源文件,进入它仍然是一个好习惯。)

isValid has to be defined or declared before you call it.在调用之前必须定义或声明isValid It is a matter of taste whether to define a static function ahead of the calling code, like this:是否在调用代码之前定义static函数是一个品味问题,如下所示:

static bool isValid(int r, int c) {
    if (r < 0 || c < 0 || r >= MAX_SIZE || c >= MAX_SIZE) return false;
    else  return map[r][c] = '0' || map[r][c] = 'x';
}

int main() {
    if (isValid(...)) etc.
}

or to declare if first and define it later, like this:或者先声明 if 然后再定义,如下所示:

static bool isValid(int r, int c);

int main() {
    if (isValid(...)) etc.
}

static bool isValid(int r, int c) {
    if (r < 0 || c < 0 || r >= MAX_SIZE || c >= MAX_SIZE) return false;
    else  return map[r][c] = '0' || map[r][c] = 'x';
}

I prefer the second method myself, but many people prefer the first.我自己更喜欢第二种方法,但很多人更喜欢第一种。

The disadvantage with this method (and the advantage with Arty's method) is that isValid doesn't have access to variables declared in main .此方法的缺点(以及 Arty 方法的优点)是isValid无法访问main声明的变量。 So it can't see MAX_SIZE or map .所以它看不到MAX_SIZEmap So in this case you need to declare these variables outside the main function too, making them static for the same reason.因此,在这种情况下,您也需要在main函数之外声明这些变量,出于同样的原因将它们设为static

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

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