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 .
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <cctype>
#include <sstream>
#include <iomanip>
usingnamespace 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
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;
/* ... */
}
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
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 ,
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.
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.