-1

I want to print the values of the multi-dimensional vector a using pass by pointer. Can anyone please let me know what is wrong in my program? I am getting a lot of errors.

#include <iostream>
#include <vector>

using namespace std;

class foo{
    private:
    vector <int> v;
    public:
    foo(vector<vector <int> >**);
};

foo :: foo(vector<vector<int> >** a)
{
    cout << a[0][0] <<endl;
    
    //cout<<a->size()<<endl;
}

int main()
{
    vector<vector<int>> a{{1,2,3,4},{5,6,7,8}};
    foo f(&a);
    
}

Thank you!

uuuuuuuuuu
  • 121
  • 5
  • Please show the errors you are getting. You aren't passing by reference you are using pointers so you need to use `(*a)[0][0]` – Alan Birtles Oct 21 '20 at 06:13
  • @AlanBirtles That was a silly mistake, sorry. – uuuuuuuuuu Oct 21 '20 at 06:15
  • It looks like you've been reading about arrays and are trying to apply what you read to vectors. Get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). (And there is absolutely no reason to use a class here.) – molbdnilo Oct 21 '20 at 06:16
  • @molbdnilo Actually this class structure is used in a different program that I am writing. Yes this class is not needed here but I need this concept in a different program. And yes I am tryng to replicate the multi dimensional array thing in vectors. – uuuuuuuuuu Oct 21 '20 at 06:20

1 Answers1

2

You are confusing some things here:

  • vector<vector<int> >** is a pointer to a pointer of vector<vector<int> >.
  • In foo f(&a); the expression &a is vector<vector<int> >* (pointer to vector of vectors of int)
  • A reference would be vector<vector<int> > &.

You would have foo like this:

//     Reference here VVV
foo(vector<vector<int> > &a)

and simply call like this:

foo f(a);

(without taking the address of a)

Lukas-T
  • 11,133
  • 3
  • 20
  • 30