problem with friend

0

I have a problem with the compiler g ++ that shows me an error "po_bi was not declared in this scope" however when compiling with msvc compiles me without problem, how can I solve this error? The code consists of 3 parts: complex.h, complex.cpp and main.cpp.
complex.h

#if !defined(_COMPLEJO_H_)
#define _COMPLEJO_H_
class CComplejo {

    friend CComplejo po_bi(const double mod, const double alfa);

private:
    double real;
    double imag;
public:

    explicit CComplejo(const double r = 0, const double i = 0)
            : real(r), imag(i) {}
};

#endif // _COMPLEJO_H_

complejo.cpp

#include <iostream>
#include <cmath>
#include "complejo.h"

CComplejo po_bi(const double mod, const double alfa)
{
    return CComplejo(mod * cos(alfa), mod * sin(alfa));
}

main.cpp

#include <iostream>
#include "complejo.h"

int main()
{
    CComplejo d;

    double mod = 9.9;
    double alfa = 4.5;

    d = po_bi(mod, alfa);

}
    
asked by theriver 26.12.2018 в 19:33
source

1 answer

1

The solution is to declare the function po_bi after the declaration of the class in the complex file.h such that:

#if !defined(_COMPLEJO_H_)
#define _COMPLEJO_H_
class CComplejo {

    friend CComplejo po_bi(const double mod, const double alfa);

private:
    double real;
    double imag;
public:

    explicit CComplejo(const double r = 0, const double i = 0)
            : real(r), imag(i) {}
};
CComplejo po_bi(const double,const double);

#endif // _COMPLEJO_H_
    
answered by 26.12.2018 / 20:15
source