Passing class as parameter

Hello , this is the program I made to calculate matrix operations , I did most of the arithmetic part already and now I'm just changing the output of the program so when I use the display() function I can see what matrix I read .

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <cctype>
#include <sstream>
#include <iomanip>
using namespace std;

class store{
		
	public:
		int matrix[30][30];
		int row,column; 
		 
	};

void addition(store,store,store);
void multiplication();
void transpose();
void display();
void menu();
string readname();
store readmatrix1(string);
store readmatrix2(string);

int main()
{
	int choice;
	store m1,m2,m3;
	 menu();
	

	cin>> choice ;

	switch (choice)
	{
	case 1:
		addition(&m1 , &m2,&m3) ;//problem here
		main();

	case 2:
		multiplication();
		menu();

	case 3:
		transpose() ;
		menu();
	
	case 4:
		display();
		menu();
	
	case 5:
		cout<<"Thank you for using the program !";
		cout<<"\nPlease press any button to continue.";
		cin.get();
		cin.get();
		exit (1);

	default :
		cout<<"Wrong input !";
		cout<<"\nPlease press any button to go back to menu.";
		cin.get();
		cin.get();
		system("cls");
		menu();
		
	}
	
	return 0;
}


The problem is it show me an error (several in fact)

error C2664: 'addition' : cannot convert parameter 1 from 'store *' to 'store'

error C2665: 'addition' : none of the 2 overloads could convert all the argument types

I have no idea why the &m1 becomes store* type and if I erase the & in &m1 it shows this error

error C2665: 'addition' : none of the 2 overloads could convert all the argument types

void addition(store &m1 ,store &m2, store &m3)//this is the declaration of addition function

Please help.
addition() should probably look like:
 
void addition(const store &a, const store &b, store &c);

Can you please explain why we declare the function again instead of calling it ?
Your addition function takes objects:

void addition(store,store,store);

You are passing pointers:

addition(&m1 , &m2,&m3) ;//problem here

Note that &m1 gives you a pointer to m1. IE, it's type is store*, not store. Hence your error.

Possible solutions:

1) Change the function to take pointers rather than objects:
void addition(store*,store*,store*);

or

2) Pass objects instead of pointers:
addition(m1 , m2, m3);
Last edited on
closed account (o1vk4iN6)
There's also going to be a problem when it is compilable. Your switch cases aren't ended with a break so every case below the one selected will be run.

1
2
3
4
5
6
7
8
9
10
11
12
switch (choice)
{
case 1:
	addition(&m1 , &m2,&m3) ;//problem here
	main();
        break;
case 2:
	multiplication();
	menu();
        break;
/* ... */
}
closed account (zb0S216C)
@takzee: Never invoke main().

Wazzak
Last edited on
The program compiled , but then I have unresolved externals...

Error 4 error LNK2019: unresolved external symbol "void __cdecl addition(class store *,class store *,class store *)" (?addition@@YAXPAVstore@@00@Z) referenced in function _main C:\Users\TakZee\documents\visual studio 2010\Projects\Assigntment 1\Assigntment 1\Matrix.obj Assigntment 1

what on earth ?
closed account (zb0S216C)
You never gave the linker the definition for addition(). If you plan to use a function, make sure it's complete.

Wazzak
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
47
48
49
50
51
52
53
54
55
void addition(store*,store*,store*);
void multiplication(store*,store*,store*);
void transpose(store*);
void display(store*,store*,store*);
void menu();
string readname();
store readmatrix1(string);
store readmatrix2(string);

int main()
{
	int choice;
	store m1,m2,m3;
	 menu();
	

	cin>> choice ;

	switch (choice)
	{
	case 1:
		addition(&m1,&m2,&m3) ;
		menu();

	case 2:
		multiplication(&m1,&m2,&m3);
		menu();

	case 3:
		transpose(&m3) ;
		menu();
	
	case 4:
		display(&m1,&m2,&m3);
		menu();
	
	case 5:
		cout<<"Thank you for using the program !";
		cout<<"\nPlease press any button to continue.";
		cin.get();
		cin.get();
		exit (1);

	default :
		cout<<"Wrong input !";
		cout<<"\nPlease press any button to go back to menu.";
		cin.get();
		cin.get();
		system("cls");
		menu();
		
	}
	
	return 0;
}


This is the what I did after reading xerzi's info ,

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
void addition(store &m1 ,store &m2, store &m3)
{
	int matrix1[30][30],matrix2[30][30],total_matrix[30][30];
	int row1,row2,column1,column2,row,column;
	string name ,name2;
	char decision;

	cout<<"You are using addition function now . \n";

	cin.ignore();
	name=readname();
	name2=readname();
	
	row1=readmatrix1(name).row;
	column1=readmatrix1(name).column;
	cout<<name<<" had been read !"<<endl;
	
	row2=readmatrix2(name2).row;
	column2=readmatrix2(name2).column;
	cout<<name2<<" had been read !"<<endl; 



					if(row1!=row2 || column1!=column2)
					{
						cout<<"The matrix cannot be added because they are in different size !";
					}
					else 
					{
						row=row1=row2;

						column=column1=column2;

								for(int a = 0 ; a<row; a++)
								{
									for(int b = 0 ; b<column; b++)

										{
											m1.matrix[a][b]=readmatrix1(name).matrix[a][b];

											m2.matrix[a][b]=readmatrix2(name2).matrix[a][b];	

											m3.matrix[a][b]=m1.matrix[a][b] + m2.matrix[a][b];
										}
								}

						cout<<"The result of addition of"<<name<<" and "<<name2<<" is :\n";

									for(int a= 0 ; a<row ;a++)
									{
										for(int b=0;b<column;b++)
										{
										
											cout<<setw(2);
											cout<<m3.matrix[a][b];
											
											
											if(b==column-1)
											{
												cout<<endl;
											}
											
										}
									}

						cout<<"\nOperation completed!"<<endl;
					}
	cout<<"\nDo you want to continue the function[Y/N] ?"<<endl;
		cin>>decision;

		switch (decision)
		{
			case 'Y':
			case 'y':
				system("cls");
				addition(&m1,&m2,&m3);
			case 'N':
			case 'n':
				cout<<"\nReturning to menu\n";
				cin.get();
				break;
				system("cls");
			default:
				cout<<"\nWrong input, returning to menu.\n";
				break;
				system("cls");

		}
}
This is the addition function . Please shine some light on me .
You have two different addition functions:

1
2
3
4
5
6
void addition(store*,store*,store*);  // function 1

void addition(store &m1 ,store &m2, store &m3) // function 2
{
  // ...
}


Note that they are different because they have different parameter lists. function 1 takes pointers store*, function 2 takes references store&.

You are calling function1 in your code, but you never give function1 a body. Only function2 has a body -- but it is never being called.

Solution:
- make sure your prototype and function body match exactly. If the prototype takes pointers, make your function body also take pointers. Or if you want it to take references, make them both take references.
Last edited on
@Disch

If you live near me , I'm just gonna come to you and give you a kiss ! Thanks !

And thank you to everyone that gave me some directions too !
Your problem seems to be your confusion about the use of the & symbol in parameter passing.

You declare it here:
 
void addition(store,store,store);


That means it accepts three store objects by value (makes a copy of them).

BUT you call your function like this:
 
addition(&m1 , &m2,&m3) ;//problem here 


Instead of passing objects by value, you are passing pointers to your objects.

Pointers are really not appropriate for this situation.

I think what you may have done is confuse where to place your & symbols. You put them in when you called your function and not where you declared it. This is the wrong way round. You should use them when you when you declare the function and not when you call it.

So do 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
// Forward Declarations

void addition(store&, store&, store&); // declare function to accept reference (not copies)
void multiplication();
void transpose();

// ... etc ...

int main()
{
	// ... etc ...
	
	switch (choice)
	{
	case 1:
		addition(m1, m2, m3); // Don't use & here 
		main(); // Dont ever do this.... find another way!!!

	case 2:
		multiplication();
		menu(); //  bad bad bad... :)

	// ... etc ...
	
}

// Define the functions

void addition(store &m1 ,store &m2, store &m3) // this must match the declaration above
{
	// ... etc ...
}
Topic archived. No new replies allowed.