0

main #include

#include "Polar.hpp"
using namespace std;
int main(void)
{
Polar points[2];
for(int i = 0; i<2;i++)
{
float r, theta;
cin>>r>>theta;
points[i]=Polar(r,theta);
}
const Polar sum = points[0] + points[1];
cout<<sum<<"=="<<points[0]<<"+"<<points[1]<<endl;
return 0;
}

Polar.hpp

#include <iostream>
#include <math.h>
#include <cmath>
#include <string>
#ifndef POLAR_H
#define POLAR_H
using namespace std;

class Polar
{
public:
    Polar(void);//theta in degrees
    Polar(const float r , const float theta);

    float radius(void) const;
    float angle(void) const;
    friend Polar operator +(Polar p1,Polar p2);
    friend ostream& operator<<(ostream & os, const Polar & point);

    private:
        float r,theta,_r,_theta;
};

#endif // POLAR_H

Point.hpp

#include <iostream>
#include "Polar.hpp"
#include <math.h>
#include <cmath>
#include <string>
using namespace std;

Polar::Polar(void)
{
 _r=_theta=0.0;//theta in degrees
}

Polar::Polar(const float r , const float theta)
{
_r=r;
_theta=theta;
}

 float Polar:: radius(void) const
{
return _r;
}
    float Polar::angle(void) const
{
return _theta;
}
ostream & operator<<(ostream & os, const Polar & point)
{
    os<<"(" << point.radius()<<"," <<point.angle() << ")";
    return os;
}
Polar operator +(Polar p1,Polar p2)
{      float x,y;
       Polar p3;
    x=(p1._r*cos(p1._theta*3.14159/180))+                                                         (p2._r(cos(p2._theta*3.14159/180)));

    y=(p1._r*sin(p1._theta*3.14159/180))+(p2._r*     (sin(p2._theta*3.14159/180)));
p3._r=sqrt(x*x+y*y);
   p3._theta=atan(y/x)*180/3.14159;

return p3;
}

my program include the main programm and a class my programm has a error like main.cpp:(.text+0x181): undefined reference to Polar::Polar()' main.cpp:(.text+0x292): undefined reference toPolar::Polar(float, float)' main.cpp:(.text+0x398): undefined reference to `operator+(Polar, Polar)'

i really dont know how to fix it : (

Kelvin Ng
  • 31
  • 1
  • It is strange that you would put an implementation of a class into a header file. But if you insist on doing it this way for some reason, at least `#include` that file somewhere. Currently, it's not part of your build at all. – Igor Tandetnik Nov 28 '16 at 04:01
  • Why would you implement the file in the HPP instead of CPP? If I were you. I'd copy a whole code from Point.hpp into the Polar.cpp, and then I'd create a Point that extends the class Polar if needed. – mutantkeyboard Nov 28 '16 at 09:53

0 Answers0