0
    64.minimum-path-sum.cpp: In function ‘int main()’:
    64.minimum-path-sum.cpp:67:23: error: cannot bind non-const lvalue reference of type ‘std::vector<std::vector<int> >&’ to an rvalue of type ‘std::vector<std::vector<int> >’
       67 |         if(minPathSum(vector<vector<int>> {{1 , 2, 3}}) == 12)cout << "ACC\n";
          |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    64.minimum-path-sum.cpp:57:46: note:   initializing argument 1 of ‘int minPathSum(std::vector<std::vector<int> >&)’
       57 |         int minPathSum(vector<vector<int>> & grid) {
          |    

                    ~~~~~~~~~~~~~~~~~~~~~~^~~~




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

 
int main(){

vector<vector<int>> c{{1 , 2, 3}};
if(minPathSum(c) == 12)cout << "ACC\n"; //No ERROR
else cout << "WA\n";

if(minPathSum(vector<vector<int>> {{1 , 2, 3}}) == 12)cout << "ACC\n"; // ERROR
else cout << "WA\n";
}

What is the difference between these 2 approach of passing a 2d vector as argument ??

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • You cannot pass anonymous vector to a function accepting a reference. First store the vector in some variable and pass that variable, it should work. – kiner_shah Dec 19 '21 at 10:45
  • 5
    Very likely the `minPathSum` function doesn't take its argument by value, by constant reference, or by rvalue reference. Hard to say anything for certain without a proper [mre]. – Some programmer dude Dec 19 '21 at 10:45
  • 2
    Also please take some time to read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) And note that [`using namespace std;` is a bad habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Dec 19 '21 at 10:46
  • To accept temporaries minPathSum should have an overload minPathSum(std::vector>>&& rhs). Or if you allow for some extra copying : minPathSum(const std::vector>& rhs). – Pepijn Kramer Dec 19 '21 at 10:59

1 Answers1

2

You are calling the function minPathSum creating a temporary object of the type std::vector<vector<int>> using a braced init list.

So the compiler issues an error message that you are trying to bind a temporary object with a non-coonstant lvalue reference.

Just declare the function parameter with the qualifier const

int minPathSum( const vector<vector<int>> & grid);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335