c++ - 为什么我的功能在使用 goto 时给我一个 "expected primary-expression before ' }' token"?

标签 c++ debugging goto

我的函数在使用 goto 时给出“'}' 标记之前的预期主表达式”,我不知道为什么。

这段代码在我将其放入函数之前在 main 中按原样工作。

当我将“goto”替换为“break”时它起作用了,但我需要知道这是为什么。

void fileInputLoop(ifstream& inputFile){
    do{

        cout << "Enter data file name: ";
        getline(cin, fileName);
        previousFileName = fileName;
        // The user will press enter to exit data input 
        if(fileName == ""){
            // If no file name is entered, exit this input loop
            goto skip_data_input_loop;

        }else{
            // Check to see if input is an existing file
            inputFile.open(fileName);
            if(!inputFile.is_open()){
                cout << "File is not available." << endl;
            }else{
                // FILE IS OPEN, DO SOMETHING WITH IT
                ReadData(inputFile);
                inputFile.close();
            }
        }
        // If a second++ file is read in, the previous file will be set accordingly
        // This is to track if a duplicate is from the same file or a new file
        previousFileName = fileName;

    }while(true);
    skip_data_input_loop:
}

最佳答案

问题是标签是用来标记一个语句的。换句话说,您不能没有后面没有声明的标签。

根据我的评论,您可以通过在标签后添加一个空的“null”语句来解决它:

skip_data_input_loop: /* Empty statement using the semicolon */ ;

关于c++ - 为什么我的功能在使用 goto 时给我一个 "expected primary-expression before ' }' token"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52810452/

相关文章:

c++ - 多平台包括最佳实践 (qt)

c++ - 在 C++ 中为每个平台定义函数的首选方法是什么

c# - 像在 java 中一样在 C# 中继续 <label>

c++ - 在跨平台库上工作

javascript - 如何调试网站的 IE8 客户端问题?

c - GDB “info frame” 输出的含义?

android - 调试 gradle 任务 compileReleaseJavaWithJavac

c - C 预处理器中指向 void 的指针

c# - 这是在 C# 中正确使用 GOTO 吗?

c++ - 将字符串传递给 ns3::Ipv4AddressHelper::SetBase 方法