I'm writing a simple C++ class called Vector2f. I have a class file called Vector2f.cpp and a header file called Vector2f.h.
My Vector2f
class has a function called abs
which returns a new Vector2f
with the absolute value of each of the components of the original Vector2f
. I am using the cmath library. However when I try to use cmath's abs
function, it thinks I'm referring to some undefined function Vector2f::abs(float)
rather than cmath's abs
function. Why is there a naming conflict here? Shouldn't C++ be able to resolve that a function called abs
that takes a float is only defined in cmath and not in Vector2f.h
Here is my code:
My header file:
//Vector2f.h
#ifndef VECTOR2F_H
#define VECTOR2F_H
class Vector2f
{
private:
float x;
float y;
public:
Vector2f(float x, float y);
float length();
float dot(Vector2f r);
Vector2f normalized();
Vector2f rot(float angle);
Vector2f add(Vector2f r);
Vector2f add(float r);
Vector2f sub(Vector2f r);
Vector2f sub(float r);
Vector2f mul(Vector2f r);
Vector2f mul(float r);
Vector2f div(Vector2f r);
Vector2f div(float r);
Vector2f abs();
float getX();
float getY();
};
#endif // VECTOR2F_H
My class file:
//Vector2f.cpp
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif // M_PI
#include "Vector2f.h"
#include <cmath>
Vector2f::Vector2f(float x, float y)
{
this -> x = x;
this -> y = y;
}
float Vector2f::length()
{
return (float)sqrt(x * x + y * y);
}
float Vector2f::dot(Vector2f r)
{
return x * r.getX() + y * r.getY();
}
Vector2f Vector2f::normalized()
{
float length = Vector2f::length();
float xnormal = x/length;
float ynormal = y/length;
return Vector2f(xnormal, ynormal);
}
Vector2f Vector2f::rot(float angle)
{
double rad = angle * M_PI / 180.0;
double c = cos(rad);
double s = sin(rad);
return Vector2f((float)(x * c - y * s), (float)(x * s + y * c));
}
Vector2f Vector2f::add(Vector2f r)
{
return Vector2f(x + r.getX(), y + r.getY());
}
Vector2f Vector2f::add(float r)
{
return Vector2f(x + r, y + r);
}
Vector2f Vector2f::sub(Vector2f r)
{
return Vector2f(x - r.getX(), y - r.getY());
}
Vector2f Vector2f::sub(float r)
{
return Vector2f(x - r, y - r);
}
Vector2f Vector2f::mul(Vector2f r)
{
return Vector2f(x * r.getX(), y * r.getY());
}
Vector2f Vector2f::mul(float r)
{
return Vector2f(x * r, y * r);
}
Vector2f Vector2f::div(Vector2f r)
{
return Vector2f(x / r.getX(), y / r.getY());
}
Vector2f Vector2f::div(float r)
{
return Vector2f(x / r, y / r);
}
Vector2f Vector2f::abs()
{
//I get the error, "error: no matching function for call to 'Vector2f::abs(float&)'", here
//when trying to call abs(x) and abs(y)
float xabs = abs(x);
float yabs = abs(y);
return Vector2f(xabs, yabs);
}
float Vector2f::getX()
{
return x;
}
float Vector2f::getY()
{
return y;
}
My main file:
//main.cpp
#include <iostream>
#include "Vector2f.h"
using namespace std;
int main()
{
Vector2f a(1.0f,2.0f);
cout<<a.getX()<<','<<a.getY()<<endl;
cout<<a.abs()<<endl;
return 0;
}
Any help would be appreciated.
Edit:
Error message:
error: no matching function for call to 'Vector2f::abs(float&)'
at line 80:
float xabs = abs(x);
error: no matching function for call to 'Vector2f::abs(float&)'
at line 81:
float yabs = abs(y);