通过引用传递2D矢量

时间:2014-03-15 04:12:42

标签: c++ vector reference

我还没有触及c ++一段时间,我在通过引用传递2D向量时遇到了一个非常奇怪的问题,它在main函数中描述:

class Solution {
public:
    vector<vector<int> > permute(vector<int> &num) {


    vector<vector<int> > result;
    //I intentionally omit the codes for permutation,
    //only pushing the original one to the result
    result.push_back(num);

    return result;


    }

    //print the permutation, I want to pass by referrence
    void printPerm(vector<vector<int> >  &result) {

    for(int i=0; i<result.size(); i++) {

        for(int j=0; j<result[i].size(); j++) {

            cout << result[i][j]<<" ";
            }
        }

    }

};

int main() {

    vector<int> old;
    old.push_back(1);
    old.push_back(2);
    old.push_back(3);

    Solution sol;

    //this gives me error for no matching function
    //call, candidate argument doesn't match
    sol.printPerm((sol.permute(old))); 

    //but the one below works!

    vector<vector<int> > result = sol.permute(old);

    sol.printPerm(result);


}

1 个答案:

答案 0 :(得分:1)

您需要将const添加到结果中以允许绑定到右值

 void printPerm(const vector<vector<int> >  &result) 
 //             ^^^^^^