0

My program simply is to increase the salary int the emp class throw the the function increase but I'm having this error int the call function line from the line : No suitable constructor to convert from int to emp

here 's my code :

#include <iostream>
#include <string>

using namespace std;

class emp
{
public:
int salary;
};

void increase(emp x,emp y)
{
    x.salary+=100;
y.salary+=250;

}

int main()
{
int value=0;
emp fst, scnd;
cin >> fst.salary >> scnd.salary;
increase(fst.salary,scnd.salary);
cout << fst.salary << endl << scnd.salary << endl; 

cin >> value;
return 0;
}

3 Answers3

1

increase expects two emps as parameters, yet you pass in two ints.

Change

increase(fst.salary,scnd.salary);

to

increase(fst,scnd);

Your next question is going to be why the values don't change, so to save you the trouble - it's because you're passing by value, effectively changing copies of your original objects. You'll need to pass by reference:

void increase(emp& x,emp& y)
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

increase(fst.salary,scnd.salary); should be increase(fst,scnd);, void increase(emp x,emp y) ... should be void increase(emp& x,emp& y) ...

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

You need to pass emp not int. Further, you are passing parameters by value. Use this instead:

void increase(emp &x,emp &y)

And pass the struct variables; i.e. fst and scnd instead of fst.salary and scnd.salary. Refer this question for better understanding.

Community
  • 1
  • 1
KodingKid
  • 971
  • 9
  • 14