600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 报错:non-const lvalue reference to type ‘std::string‘ cannot bind to

报错:non-const lvalue reference to type ‘std::string‘ cannot bind to

时间:2023-07-29 03:20:27

相关推荐

报错:non-const lvalue reference to type ‘std::string‘ cannot bind to

小编在写LeetCode–22.括号生成的时候发现传值的时候一个小问题,代码如下

class Solution {public:vector<string> res;vector<string> generateParenthesis(int n) {dfs("", n, n);return res;}// 这里必须是const否则报错// void dfs(const string& tmp, int left, int right)void dfs(string& tmp, int left, int right) {if(left < 0 || left > right)return ;if(left == 0 && right == 0){res.push_back(tmp);return ;}dfs(tmp + '(', left - 1, right);dfs(tmp + ')', left, right - 1);}};

下面是该段代码给出的报错,大致意思是不能对临时变量加引用。如果要对临时变量加引用的话,必须在其前面加上Const,因为如果不加const的话,编译器会认为下面代码会修改这个临时变量,而且这个变量将会被返回并且对代码有一定影响。总的来说就是,C++编译器加入了临时变量不能作为非const引用的限制

Line 63: Char 13: error: non-const lvalue reference to type 'std::string' (aka 'basic_string<char>') cannot bind to a value of unrelated type 'const char [1]'dfs("", n, n);^~Line 68: Char 22: note: passing argument to parameter 'tmp' herevoid dfs(string& tmp, int left, int right)^

下图是从隔壁搬运的,这是链接,说的很好添加链接描述

报错:non-const lvalue reference to type ‘std::string‘ cannot bind to a value of unrelated 不能对临时变量加引用

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。