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);
The compiler does an unqualified lookup of abs. What happens then depends on what you include and if there is a using declaration (and interestingly on the compiler). Everything in cmath is defined in namespace std, so you have to qualify your call with std::.
Vector2f Vector2f::abs()
{
//I get the error, "undefined reference to `Vector2f::abs(float)'", here
//when trying to call abs(x) and abs(y)
float xabs = std::abs(x);
float yabs = std::abs(y);
return Vector2f(xabs, yabs);
}
If you have a using std::abs or using namespace std somewhere before Vector2d::abs, you can qualify with ::abs only. Compilers are allowed to declare C functions such as abs in the global namespace in addition to namespace std, so depending on the compiler using ::abs may work or not without using declarations at all. Clang 3.8 accepts the code, gcc does not.
PS: I would expect a function Vector2f::abs to compute the vector norm and not a vector with absolute values of the original components. But then I am not a mathematician.
Related
I am making a module/library with a vector class, and I want it to do it properly.
class Vector3 {
public:
float x, y, z;
public:
Vector3();
Vector3(float a, float b, float c);
float length();
void normalize();
Vector3* dotproduct(Vector3 *rhs);
Vector3* crossproduct(Vector3 *rhs);
Vector3* add(Vector3 *rhs);
Vector3* subtract(Vector3 *rhs);
};
My doubt is in how should I return a new Vector3 after an operation.
Currently, I am dynamically allocating a new Vector3 inside each operation and then I return it.
When I use the operation I have:
Vector3 *v = v2->crossproduct(v3);
Should I change my operations to:
Vector3 Vector3::crossproduct(Vector3 *rhs){
float a = y * rhs->z - z * rhs->y;
float b = z * rhs->x - x * rhs->z;
float c = x * rhs->y - y * rhs->x;
Vector3 res(a, b, c);
return res ;
}
And use:
Vector3 v = v2->crossproduct(v3);
Or will I eventually lose the vector?
Since I'm trying to make a library what is the proper way to do it?
Allocate at the stack, or in the heap?
I implemente these operations like this:
Vector3 Vector3::crossproduct(const Vector3& rhs){
float a = y * rhs.z - z * rhs.y;
float b = z * rhs.x - x * rhs.z;
float c = x * rhs.y - y * rhs.x;
Vector3 res(a, b, c);
return res ;
}
To use this operator you can simply use this syntax:
Vector v1, v2;
auto product = v1.crossproduct(v2);
The return as value is most likely optimized away by copy elision, so you dont have to worry about that. And since rhs is not modified, passing it as const ref& is the fastest way to do it.
I have a program in C++ (compiled using g++). I'm trying to apply two doubles as operands to the modulus function, but I get the following error:
error: invalid operands of types 'double' and 'double' to binary 'operator%'
Here's the code:
int main() {
double x = 6.3;
double y = 2;
double z = x % y;
}
The % operator is for integers. You're looking for the fmod() function.
#include <cmath>
int main()
{
double x = 6.3;
double y = 2.0;
double z = std::fmod(x,y);
}
fmod(x, y) is the function you use.
You can implement your own modulus function to do that for you:
double dmod(double x, double y) {
return x - (int)(x/y) * y;
}
Then you can simply use dmod(6.3, 2) to get the remainder, 0.3.
Use fmod() from <cmath>. If you do not want to include the C header file:
template<typename T, typename U>
constexpr double dmod (T x, U mod)
{
return !mod ? x : x - mod * static_cast<long long>(x / mod);
}
//Usage:
double z = dmod<double, unsigned int>(14.3, 4);
double z = dmod<long, float>(14, 4.6);
//This also works:
double z = dmod(14.7, 0.3);
double z = dmod(14.7, 0);
double z = dmod(0, 0.3f);
double z = dmod(myFirstVariable, someOtherVariable);
Trying to get code that was compilable in g++ to compile in VS2015. I looked around SO & Google with not much luck, yet cmath is documented in MSDN. I'm guessing I'm missing something really obvious or simple.
cmath is throwing a lot of errors most of the errors I'm getting during compilation, and half are in the form:
the global scope has no "<function>"
others are in the form
'<function>': redefinition; different exception specification
'<function>': identifier not found
'<function>': is not a member of "global namespace"
I don't understand why these errors are being thrown, but, if I use math.h, most of my compilation errors go away (including some in other standard libs that are crapping out, too).
Edit: As requested, the code. I'm using the sqrt & pow functions:
#include "vector.h"
#include <cmath>
using namespace vectormath;
vector::vector()
{
this->_x = 0;
this->_y = 0;
this->_z = 0;
this->_length = 0;
}
vector::vector(float x, float y, float z)
{
this->_x = x;
this->_y = y;
this->_z = z;
this->_length = sqrt(pow(_x, 2) + pow(_y, 2) + pow(_z, 2));
}
vector * vectormath::crossproduct(vector * a, vector * b)
{
vector * result = new vector();
result->_x = a->_y * b->_z - a->_z * b->_y;
result->_y = a->_z * b->_x - a->_x * b->_z;
result->_z = a->_x * b->_y - a->_y * b->_x;
return result;
}
point::point()
{
this->_x = 0.0;
this->_y = 0.0;
this->_z = 0.0;
}
point::point(float x, float y, float z)
{
this->_x = x;
this->_y = y;
this->_z = z;
}
float vectormath::dotproduct(vector a, vector b)
{
return a._x * b._x + a._y * b._y + a._z * b._z;
}
vector * vectormath::add(point * a, vector * b)
{
vector * c = new vector();
c->_x = a->_x + b->_x;
c->_y = a->_y + b->_y;
c->_z = a->_z + b->_z;
return c;
}
Edit: and vector.h
namespace vectormath
{
struct vector
{
float _x;
float _y;
float _z;
float _length;
vector();
vector(float x, float y, float z);
};
struct point
{
float _x;
float _y;
float _z;
point();
point(float x, float y, float z);
};
vector * crossproduct(vector*, vector*);
float dotproduct(vector a, vector b);
vector * add(point * a, vector * b);
}
The difference between
#include <math.h>
and
#include <cmath>
is that the former puts things like sqrt and pow into the global namespace (i.e., you refer to them just by saying sqrt or pow) and the latter puts them into namespace std (i.e., you refer to them by saying std::sqrt or std::pow).
If you want not to have to prefix them with std:: all the time, you can put individual ones in the global namespace explicitly:
using std::sqrt;
or (though this is not recommended) you can pull in the whole of std like this:
using namespace std;
The trouble with that is that there are a lot of names in std and you probably don't really want them all.
I am making a program that converts rectangular coordinates into polar coordinates and whenever I go to run the program it tells me that the "angle" is undeclared even though I am sure I have declared it. As well I know that the program isn't returning anything, I just want to be able to run it for now.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
double random_float(double min, double max);
void rect_to_polar(double x, double y, double &distance, double &angle);
int main() {
double x, y;
x = random_float(-1, 1);
y = random_float(-1, 1);
rect_to_polar(x, y, distance, angle);
}
double random_float(double min, double max) {
unsigned int n = 2;
srand(n);
return ((double(rand()) / double(RAND_MAX)) * (max - min)) + min;
}
void rect_to_polar(double x, double y, double &distance, double &angle) {
const double toDegrees = 180.0/3.141593;
distance = sqrt(x*x + y*y);
angle = atan(y/x) * toDegrees;
}
You did not declare anything called angle in your main(), but still used the name angle there. Thus the error.
You might want to read up on scopes.
You should declare distance and angle in your main.
int main() {
double x, y, angle, distance;
x = random_float(-1, 1);
y = random_float(-1, 1);
rect_to_polar(x, y, distance, angle);
}
I am trying to get area of circle using my program. But area is not coming in decimals.
#include<iostream>
using namespace std;
float AreaOfCircle(float r);
int AreaOfCircle(int r);
int main()
{int rad;
cout<<"Enter the Radius of Crircle: ";
cin>>rad;
cout<<"The Are of the Cirlcle: "<<AreaOfCircle(rad);
}
float AreaOfCircle(float r)
{
int area=0;
area=2*3.1456*r*r;
return area;
}
int AreaOfCircle(int r)
{
int area=0;
area=2*3.1456*r*r;
return area;
}
But I need answer to some decimal point.
You're not calling the float version of the method.
Either declare your variable as float
float rad;
or cast it to float before you call the method.
AreaOfCircle((float)rad);
You also need to use float instead of int inside the overloaded method:
float AreaOfCircle(float r)
{
float area=0; // <--- float here
area=2*3.1456*r*r;
return area;
}
Also:
area = pi * r * r
length = 2 * pi * r
pi ~= 3.1415
In addition to answer by #Luchian, you need to change the returned value to a float:
float AreaOfCircle(float r)
{
int area=0; // <<----- float area = 0;
area=2*3.1456*r*r;
return area;
}
change to:
float AreaOfCircle(float r)
{
float area=0;
area=2*3.1456*r*r;
return area;
}
or just:
float AreaOfCircle(float r) { return 2*3.1456*r*r; }
The compiler will call the overload it feels is the best match to the types of parameters it is passed. Because you passed an int, it assumed you wanted the int version.
By casting to a float as Luchian suggested (or using a float in the first place) you are telling the compiler that you intend the parameter to be a float type - thus, it picks the float version.