简体   繁体   English

错误:控制到达非无效 function [-Werror=return-type] C++ 错误的结尾

[英]EROR: control reaches end of non-void function [-Werror=return-type] error in C++

I need help to solve this problem.我需要帮助来解决这个问题。 I am getting an error which is given below and code is also given.我收到下面给出的错误,并且还给出了代码。 This program is to find whether bracket is balanced or not .这个程序是检查支架是否平衡

#include <bits/stdc++.h>
#include<stack>
using namespace std;

// Complete the isBalanced function below.
string isBalanced(string s) {
    stack <char> st;
    for(auto c:s){
        switch (c){
            case '(':
            case '{':
            case '[':
                  st.push(c);
                   break;
            case '}':
                if(st.empty() || st.top()!='{' )
                    return "NO";
                st.pop();
                break;
            case ']':
                if(st.empty() || st.top()!='[')
                    return "NO";
                st.pop();
                break;
            case ')':
                if(st.empty() || st.top()!='(')
                    return "NO";
                st.pop();
                break;
            default: break;
        }
    }

}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));
    
    int t;
    cin >> t;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    for (int t_itr = 0; t_itr < t; t_itr++) {
        string s;
        getline(cin, s);
        
        string result = isBalanced(s);
        
        fout << result << "\n";
    }
    
    fout.close();
    
return 0;
}




this is a program to check bracket is balanced or not.

this is the code I have written and it's throwing me the error.这是我编写的代码,它给我带来了错误。 Please help me to fix this problem.请帮我解决这个问题。 I have tried by adding stack header file but still, I am getting same error.我尝试通过添加堆栈 header 文件,但仍然遇到同样的错误。

***Solution.cpp: In function ‘std::__cxx11::string isBalanced(std::__cxx11::string)’:
 Solution.cpp:7:18: error: control reaches end of non-void function [-Werror=return-type]
    stack <char> st;
              ^~
 cc1plus: some warnings being treated as errors***

No return statement will executed in your isBalanced function when the for loop is completed without executing return statement inside that.for循环完成而不在其中执行return语句时,将不会在您的isBalanced function 中执行任何return语句。

It seems you should add code to check if the stack st is empty and return proper things according to its result after the loop.看来您应该添加代码来检查堆栈st是否为空,并在循环后根据其结果返回正确的内容。

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

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