Passing 2D vector to a function

Hi,

I am using vector to vector to define 2D matrix and then I am trying to pass pointer to this double vector to function "Display_Individual_Elements".

I am getting the following error:
1>c:\users\dk_gs\documents\visual studio 2010\projects\matrixmultiplication\matrixmultiplication\matrixmultiplication.cpp(43): error C2440: 'return' : cannot convert from 'std::vector<_Ty>' to 'float &'

I would appreciate some help here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "stdafx.h"
#include <iostream>
#include <vector>
#include "Function_Declare.h"
using namespace std;



float& Display_Individual_Elements(vector<float> *,int, int);

int main(){
	int m1, n1, m2, n2;

	cout<<"Enter the dimension of first matrix \n";
	cin>>m1>>n1;
	// Declaring matrix of size (m x n)
	vector<vector<float>> A(m1, vector<float>(n1,0));    
	//Display_Individual_Elements(A, m1, n1);

	cout<<"Enter the dimension of second matrix \n";
	cin>>m2>>n2;


	// Declaring matrix of size (m2 x n2)
	vector<vector<float>> B(m2, vector<float>(n2,0)); 

	vector<vector<float>> *ptrB;
	ptrB = &B;

	int i, j;	
	for (i=1;i<=m2;i++){
		for (j=1;j<=n2;j++){
				Display_Individual_Elements(ptrB, i, j);
		}
	}



	system("pause");
	return 0;
}

float& Display_Individual_Elements(vector<vector<float>> *p, int m, int n)
{
	cout << p->at(m).at(n) << endl; 
}



Last edited on
p's type: vector<vector<float>>*
First [] changes the type to: vector<vector<float>>
Second [] changes the type to: vector<float>

You need a * somewhere. :)

-Albatross

P.S - You do realize that return terminates the function it's in, making your loops pointless, right?
Last edited on
The prototype in the declaration doesn't match the one of the implementation.
The function also just returns the [0][0] element (or in the case of m==0 or n==0 it results in undefined behavior).

Some other stylistic problems: you should pass the vector as a (const) reference, unless it is valid to pass 0 (which it isn't, since you're not handling that case). Even when passing a pointer, lines 25 and 26 are unnecessary. Just pass &B as the first parameter in line 28.
You also should not declare counter variables outside the for loop (as in line 38) unless you need to know the values the loop terminated at.
Thanks Albatross and Athar.

I am new to C++ and trying to learn. So, your help is highly appreciated.

I tried to modify the code and remove "return" statement and replaced that with "cout << p->at(m).at(n) << endl; "

Q 1: Why I can not use
std::cout<< p[m][n];

Still I could not figure out what I am doing wrong with:
Display_Individual_Elements(ptrB, i, j);


I am not able to understand pointer or reference for 2D or higher matrix as I am trying to do here.

Would you please explain to me:
(i) How the function and call will change if I want to pass 2D vector B as a pointer?
(ii)How the function and call will change if I want to pass 2D vector B as a reference?

For passing by reference, I believe that I should do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "stdafx.h"
#include <iostream>
#include <vector>
#include "Function_Declare.h"
using namespace std;



float& Display_Individual_Elements(vector<float> *,int, int);

int main(){
	int m1, n1, m2, n2;

	cout<<"Enter the dimension of first matrix \n";
	cin>>m1>>n1;
	// Declaring matrix of size (m x n)
	vector<vector<float>> A(m1, vector<float>(n1,0));    
	//Display_Individual_Elements(A, m1, n1);

	cout<<"Enter the dimension of second matrix \n";
	cin>>m2>>n2;


	// Declaring matrix of size (m2 x n2)
	vector<vector<float>> B(m2, vector<float>(n2,0)); 

	int i, j;	
	for (i=1;i<=m2;i++){
		for (j=1;j<=n2;j++){
	              Display_Individual_Elements(B, m2, n2);

		}
	}

	system("pause");
	return 0;
}

float& Display_Individual_Elements(vector<vector<float>> &p, int m, int n)
{
	cout << p.at(m).at(n) << endl; 

}


Anyway that is not working either.
Last edited on
Topic archived. No new replies allowed.