I made vector class in c++ with having the class in a new module, and having a separate header file. If I call any of the class functions from the main function, i get the undefined reference error, even though i have included the header file everywhere
vec2d.h
#ifndef VEC2D_H
#define VEC2D_H
class vector{
private: double x, y;
public: vector(double x, double y);
vector(int x, int y);
vector();
double getX()const;
double getY()const;
double getR()const;
double getPhi()const;
void Print() const;
void setX(double x);
void setY(double y);
void setR(double r);
void setPhi(double phi);
vector sum(const vector inv)const;
};
double Abs(const vector v);
vector sum(const vector a, const vector b);
vector minusz(const vector a, const vector b);
double dotprod(const vector a, const vector b);
vector operator *(const double alpha, const vector v);
vector operator -(const vector a, const vector b);
#endif // VEC2D_H
vec2d.cpp
#include <cmath>
#include <cstdio>
#include "vec2d.h"
vector::vector(double x, double y)
{
this->x = x;
this->y = y;
}
vector::vector(int x, int y)
{
this->x = (double)x;
this->y = (double)y;
}
vector::vector()
{
this->x = 0;
this->y = 0;
}
double vector::getX()const
{
return x;
}
double vector::getY()const
{
return y;
}
double vector::getR()const
{
return sqrt(x*x + y*y);
}
double vector::getPhi()const
{
return atan2(y, x);
}
void vector::Print() const
{
printf("[ %lf, %lf ] \n", x, y);
}
void vector::setX(double x)
{
this->x = x;
}
void vector::setY(double y)
{
this->y = y;
}
void vector::setR(double r)
{
x = r * cos(getPhi());
y = r * sin(getPhi());
}
void vector::setPhi(double phi)
{
x = getR() * cos(phi);
y = getR() * sin(phi);
}
vector vector::sum(const vector inv)const
{
vector outv;
outv.x = x + inv.x;
outv.y = y + inv.y;
return outv;
}
double Abs(const vector v)
{
return v.getR();
}
vector sum(const vector a, const vector b)
{
vector outv;
outv.setX( a.getX() + b.getX() );
outv.setY( a.getY() + b.getY() );
return outv;
}
vector minusz(const vector a, const vector b)
{
vector outv;
outv.setX( a.getX() - b.getX() );
outv.setY( a.getY() - b.getY() );
return outv;
}
double dotprod(const vector a, const vector b)
{
return a.getX() * b.getX() + a.getY() * b.getY();
}
vector operator *(const double alpha, const vector v)
{
return vector(alpha * v.getX(), alpha * v.getY() );
}
vector operator -(const vector a, const vector b)
{
return vector(a.getX() - b.getX(), a.getY() - b.getY());
}
main.cpp
#include <cstdio>
#include <cmath>
#include "vec2d.h"
#include "PontToltes.h"
using namespace std;
int main()
{
vector v(2,3);
ptoltes a(1, 2, 2);
v.Print();
return 0;
}
Could you help me please,Its probably a rookie mistake, but I don't get it.