简体   繁体   中英

error: invalid initialization of non-const reference of type ‘std::vector<int>&’ from an rvalue of type ‘std::vector<int>*’

I am new to STL in C++. I have this piece of code in which I am trying to input values in each vector of array of vectors v . When I pass this array of vectors and a number to a function happiness, it gives the following error:

 error: invalid initialization of non-const reference of type ‘std::vector<int>&’ from an rvalue of type ‘std::vector<int>*’
         int val = happiness(n,v);

The code is here:

int main(){
        int n;
        cin >> n;
        vector<int> v[10000];
        //vector<int> v(10000);
        //int val;

        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
              int temp;
              cin >> temp;
              v[i].push_back(temp);
         }

        }

        int val = happiness(n,v);

//      return 0;

}

Please let me know how should I understand this error and rectify it.

Here is the happiness function defined:

int happiness(int &num, vector<int> &vecvec[]){
       vector<int> pdp(10000,0);
       int sum = 0;
       int tempsum = -100;
       int max_curr_ind = -1;
       int i,j;
        for(i = 0; i < num; i++){
                for(j = 0; j < num; j++){
                        if(vecvec[i,j] >= tempsum && j != max_curr_ind){
                                tempsum = vecvec[i,j];
                                max_curr_ind = j;
                        }
                }
                sum = sum + tempsum;
                pdp[i,max_curr_ind] = sum;
                tempsum = -100;

        }
        cout << pdp[i,max_curr_ind] << endl;
        return pdp[i,max_curr_ind];

}

You may definitely want to replace this:

    vector<int> v[10000];
    //vector<int> v(10000);
    //int val;

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
          int temp;
          cin >> temp;
          v[i].push_back(temp);
       }
    }

with this:

vector<vector<int> > v;
for( int i=0; i<n; i++) {
    vector<int> temp;
    for( int j=0; j<n; j++) {
        int tmp;
        cin >> tmp;
        temp.push_back( tmp );
    }
    v.push_back( temp );
}

And then define:

int happiness(int &num, vector<vector<int> >& vecvec)

to be able to pass v just as-is.

Let's dissect the error message:

invalid initialization of non-const reference of type ‘std::vector<int>&’ ...
... int val = happiness(n,v);          

This tells you that your function expects a std::vector<int>& in the function call int val = happiness(n,v) .

from an rvalue of type ‘std::vector<int>*’

This tells you what you are actually providing, a std::vector<int>* . This is, because c-style arrays like vector<int> v[10000]; are just pointers. The number in the brackets just tells the compiler how much memory he should allocate on the stack to fulfill the memory requirements of this array. So if you want to pass it to your function, you can do it in several ways. Replacing the vector<int> &vecvec[] with vector<int>* vecvec will probably be the fastest fix.

However, a more suitable solution to your problem is probably using a std::vector<std::vector<int>> v; like suggested the answer of "lenik", because you can resize it using v.resize(10000) and fetch the current size with v.size() (Always use this when looping over the vector elements). You can pass it directly to your function using std::vector<std::vector<int>>& . Additionally, you don't need to pass num to your function if you resize your array to the number you entered:

...
cin >> n;
std::vector<std::vector<int>> v(n);

or

...
cin >> n;
std::vector<std::vector<int>> v;
v.resize(n);

Then your loops would be

for(int i=0; i < v.size(); i++){
        ...
        for(int j=0; j < v.size(); j++){
        ...
        }
     ...
     }

Alternatively, if you know that your array of vectors always has a constant size, you can also use a std::array<std::vector<int>, 10000> v; ( #include <array> ). It is a wrapper class around the c-style array which gives you extra functionality like storing the array's size that you can get in the same way as in the std::vector ( v.size() ).

An additional note: There is no need to pass num per reference int& . Fundamental types like int , float , double etc should always be passed by value and not per reference if you are not intending to modify their value inside the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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