Question

I'm trying to use some functions for a class I have made in the overloaded printing operator however it won't allow me to use them, saying the functions weren't declared in the scope.

In the .cpp of my class BigFloat I have a function which I wish to use:

void BigFloat::round(BigFloat& input) 
{   
    //round the number, it works fine
}

I have used this for other overloaded operators such as + and - which works fine.

This is my code for printing however including the round function gives the error.

std::ostream& operator <<(std::ostream& out, BigFloat y)
{
    round(y);
    out << y.sig << "e" << y.exp;
    return out;
}

I have declared these in the header file as

void round(BigFloat&);

and

friend std::ostream& operator <<(std::ostream& out, BigFloat y);

respectively.

Was it helpful?

Solution

If you want to call your function round() this way

std::ostream& operator <<(std::ostream& out, BigFloat y)
{
    round( y);
    //...
}

it has to be a static member function in BigFloat:

class BigFloat {
//...
public:
    static void round(BigFloat& input) 
    {   
        //...
    }
//...
}

Then you will call it this way:

std::ostream& operator <<(std::ostream& out, BigFloat y)
{
    BigInt::round( y);
    //...
}

OTHER TIPS

It looks from your code as if round() is a member function of BigFloat. If that's the case, what is it's parameter for? I'd assume it would take no parameters and instead affect the data members of the object it's invoked on. So I would change its signature & use like this:

void BigFloat::round()
{
  // Compute rounding, store result in `sig` and `exp`
}

std::ostream& operator <<(std::ostream& out, BigFloat y)
{
    y.round();
    out << y.sig << "e" << y.exp;
    return out;
}

Looks like round is a member function of BigFloat. It would need to be a static or free function to call it in the manner that you show here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top