[White Road C ++ learning] common mistakes summary

1

 


 

 

class Solution {
public:
    void __dfs(vector<string> &paths, string &path, int loc, int n, int left, int right)
        {
            if (loc == 2*n){
                paths.push_back(path);
                return;
            }
            
            if (left < n){
                __dfs(paths, path+"(", loc+1, n, left+1, right);
                //path.pop_back();
            }
            if (right < left){
                __dfs(paths, path+")", loc+1, n, left, right+1);
                //path.pop_back();
            }
            
            
        }
    vector<string> generateParenthesis(int n) {
        // 递归法来做
        vector<string> paths;
        string s;
        __dfs(paths, s, 0, n, 0, 0);
        return paths;        
    }
    
    
};

 

编译报错:Line 11: Char 34: error: cannot bind non-const lvalue reference of type 'std::__cxx11::string&' {aka 'std::__cxx11::basic_string<char>&'} to an rvalue of type 'std::__cxx11::basic_string<char>'

 

This error is a C ++ compiler restrictions on semantics.

If a non-const reference parameter is passed, c ++ compiler is reason to believe that a programmer would modify this value in the function, and this is modified after the function returns a reference to the role. But if you put a temporary variable as a non-const reference parameters passed in, due to the special nature of temporary variables, the programmer does not operate temporary variables and temporary variables may be freed at any time, so, generally speaking, modify a temporary variable There is no point, accordingly, c ++ compiler temporary variables can not be added to the semantic restrictions as a non-const reference.

Guess you like

Origin www.cnblogs.com/ACStrive/p/11573870.html