-1

I am learning C++ and I have written the below given simple program to understand the working of friend function (ignore all the complication I made by using complex syntax in the code because I am learning and I practice the syntax that I learn in programs).

The friend function is not accessing the private members of test and stu.

#include<iostream>
#include<conio.h>

class test;

class stu
{
    private:
        int z;
    public:
        stu(int z)
        {
            this->z=z;
        }
    
        friend disp(stu,test);
        
        ~stu(void)
        {
            std::cout<<"Destructor of stu class is executed!!"<<std::endl;
        }
        
};

class test{
    private:
        int x;
    public:
        test(int a)
        {
            x=a;
        }
        
        friend disp(stu,test);
        
        ~test(void)
        {
            std::cout<<"Destructor is executed!!"<<std::endl;
        }
    
};

class test2:public test
{
    private:
        int b;
    public:
        test2(int b)
        {
            this->b=b;
        }
        
        void show(void);
        
        ~test2(void)
        {
            std::cout<<"Destructor of second class executed!!"<<std::endl;
        }
};


int main()
{
    test t1(3);
    test2 t2(5);
    t2.show();
    stu s1(10);
    disp(s1,t1);
    
    return 0;
}

void test2::show(void)
{
    std::cout<<"Value of k = "<<b<<std::endl;
}

void disp(stu s2, test t2)
{
    int sum;
    sum = s2.z + t2.x;
    std::cout<<"Sum  =  "<<sum<<std::endl;
}
BZKN
  • 1,499
  • 2
  • 10
  • 25
Ahsan
  • 11
  • *"The friend function is not accessing the private members of test and stu."* - can you elaborate on this? e.g. is this causing a compile error, a runtime error, unexpected behavior, etc? Also I'd recommend reducing this code down into a [mcve]. – 0x5453 Jan 25 '22 at 21:57
  • 2
    Your `friend disp(stu, test);` lines should be `friend void disp(stu, test);` ... and you'll be needing a default constructor for `test`. – Adrian Mole Jan 25 '22 at 22:00
  • @0x5453 yeah it is giving multiple errors like (1)[Error] no matching function for call to 'test::test()' (2) [Error] ambiguating new declaration of 'void disp(stu&, test&)' (3)[Note] test::test(int) (4) [Note] candidate expects 1 argument, 0 provided (5)[Note] test::test(const test&) and both the variables are privates and are declared outside the scope of it – Ahsan Jan 25 '22 at 22:01
  • @Adrian Mole thanks now only the error 1 2 3 4 and 5 that i have mentioned here are remaining – Ahsan Jan 25 '22 at 22:05
  • One question at a time, man. – user4581301 Jan 25 '22 at 22:18
  • You will find the [member initializer list](https://en.cppreference.com/w/cpp/language/constructor) helpful. – user4581301 Jan 25 '22 at 22:20
  • @user4581301 hahaha ok – Ahsan Jan 25 '22 at 22:25

1 Answers1

0

Try to define the disp function before the main function :

void disp(stu s2, test t2)
{
    int sum;
    sum = s2.z + t2.x;
    std::cout<<"Sum  =  "<<sum<<std::endl;
}

int main()
{
    test t1(3);
    test2 t2(5);
    t2.show();
    stu s1(10);
    disp(s1,t1);
    
    return 0;
}

and change the disp function signature as:

friend void disp(stu,test);
navylover
  • 12,383
  • 5
  • 28
  • 41
  • Thanks mate not writing the return type was a mistake and but these error are not going away and i dont understand why because i have already provided the arguments In constructor 'test2::test2(int)': [Error] no matching function for call to 'test::test()' [Note] candidates are: [Note] test::test(int) [Note] candidate expects 1 argument, 0 provided [Note] test::test(const test&) [Note] candidate expects 1 argument, 0 provided – Ahsan Jan 25 '22 at 22:10