Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the "add" function showing out of scope error?

outputthis is the code in c++

I have overloaded + operator conditionally. When I use the overloaded op in main separately , it works as demanded. But I need to have it called within the add function and it throws me this error. Please help me resolve this!!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem is that add is a non-static member function and not a free function. So you must call it using a Time object.

You can solve this either by making add a free function as shown below or calling it on a Time object like t3.add(t1, t2).

class Time
{
   //your code here
};
//make add a free function 
Time add(const Time&a, const Time&b)
//-------^^^^^---------^^^^^----------->added const here
{
    return a+b;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading