-3

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

Kiran C K
  • 67
  • 7

1 Answers1

2

You are defining a global function

void display_des();

instead of the member function

void Salary::display_des();

That means display_des has no "this"- Salary-object from which it could take the member sal[]. You also don't pass it a Salary-object from outside, so which sal[] should it use?

So either you use a friend function like this:

void display_des(Salary& obj){
    obj.sal[...]...
}

Or you use a member-function, similar to this:

class Salary
{
private:
    int sal[10];

public:
    (...)
    void display_des();
    (...)
};
(...)
void Salary::display_des(){
    sal[...]...
    (...)
}
Anedar
  • 4,235
  • 1
  • 23
  • 41
  • 1
    Thought so too, but what about that `friend` keyword? – Simon Kraemer Feb 17 '16 at 10:52
  • Friend means it may access the private fields of a given Salary-object. But you dont pass it a Salary-object. – Anedar Feb 17 '16 at 10:52
  • I know what a `friend` is. Yet your answer is expecting that OP meant member functions even though he/she might have meant friend functions. – Simon Kraemer Feb 17 '16 at 10:53
  • Also your answer should be rephrased as it currently looks like OP should add `Salary::` to the function definitions inside the class definition. – Simon Kraemer Feb 17 '16 at 10:54
  • oops, thought you would be OP, added that – Anedar Feb 17 '16 at 10:55
  • But still don't understand why the constructor shows that add_details was not declared in the scope, while trying to make a call to add details – Kiran C K Feb 17 '16 at 11:07
  • Because the function definition is AFTER the class. At the time the compiler parses the class, there is no function add_details yet. You can define it afterwards, but you need at least a declaration before you use it. – Anedar Feb 17 '16 at 11:12
  • @KiranCK Adjust your program as described by Anedar and it will work. – Simon Kraemer Feb 17 '16 at 11:12
  • I did. I declared the friend class add_details with the OP in public and called the function from the constructor and here are the errors "||=== Build: Debug in test (compiler: GNU GCC Compiler) ===| In constructor 'Salary::Salary()':| error: invalid initialization of non-const reference of type 'Salary&' from an rvalue of type 'Salary* const'| error: in passing argument 1 of 'void add_details(Salary&)'| – Kiran C K Feb 17 '16 at 11:19
  • You probably tried to call `add_details(this)` while it should be `add_details(*this)`. `this` is a pointer to Salary, but your function needs a reference/ and object. – Anedar Feb 17 '16 at 11:25
  • @Anedar: Thanks. I got that now. – Kiran C K Feb 17 '16 at 11:28