I am a beginner in C++. I am learning the topic friend functions. I have the code below in which two friend functions are declared in the class and called by the constructor but an error shows that the declared friend member functions are not declared in the scope. What am I doing wrong here? here is my code:
#include <iostream.h>
class Salary
{
private:
int sal[10];
public:
friend void add_details();
void display();
friend void display_des();
Salary()
{
add_details();
}
};
void add_details()
{
int loop = 0;
for(loop=0;loop<10;loop++)
{
cin >> sal[loop];
if (sal[loop] <= 0)
{
cout << "The amount should be greater than 0" << endl;
loop = loop - 1;
continue;
}
}
}
void display_des()
{
int sal_des[10];
int loop1 = 0, loop2;
for(loop1=0; loop1<10; loop1++)
{
sal_des[loop1] = sal[loop1];
}
for (loop1=0; loop1<10; loop1++)
{
for(loop2=loop1+1; loop2<10; loop2++)
{
if (sal_des[loop1]< sal_des[loop2])
{
int temp;
temp = sal_des[loop1];
sal_des[loop1] = sal_des[loop2];
sal_des[loop2] = temp;
}
}
}
for(loop1=0; loop1<10; loop1++)
{
cout << sal_des[loop1];
}
}
int main()
{
Salary sal1;
sal1.display_des();
return 0;
}
Also, another error inside function display_des()
is shown as sal is not declared in this scope