0
votes

I am getting error "show was not declared in this scope " while compiling below program even after declaring and defining. Don't know where i am wrong. Please suggest.

Thanks

 #include < iostream >

using namespace std;

class add

{

        int x;
        int y;

        public:
                void putdata(int,int);
                void show(add);
                friend add sum(add,add);
};

void add :: putdata (int m,int n)

{

        x = m;
        y = n;
}

void add :: show(add c)

{

        cout<<c.x <<" "<<c.y<<endl;
}

add sum(add a1,add a2)

{

        add a3;
        a3.x = a1.x + a2.x;
        a3.y = a1.y + a2.y;
        return(a3);
}


int main()
{

        add p,q,r;

        p.putdata(10,15);
        r.putdata(20,25);

        r = sum(p,q);

        show(r);

        return 0;
}

~

2
Please fix your formatting - David Heffernan

2 Answers

1
votes

show is a non-static member function of add, so you need to call it on an instance of add:

p.show(r);

It doesn't make much sense like this, so you can either make it a non-member function, or remove its parameter:

show(r); // non-member

or

r.show(); // member
0
votes

You need to call the show(r) member function from within an object of add, just like, r.show(r);