The following code demonstrate simple operator overloading:
Class Position has 3 int field and a method to print them.
class Position
{
private:
int x,y,z;
public:
void print()
{
cout << x << "," << y << "," << z << endl;
}
Position (int a, int b, int c)
: x(4),
y(50),
z(23)
{
x=a;
y=b;
z=c;
}
Position operator+(const Position &other);
Position operator*=(const int &multi);
};
Operators + and *= are overloaded as such:
Position Position::operator+ (const Position &other)
{
x = x + other.x;
y = y + other.y;
z = z + other.z;
return *this;
}
Position Position::operator*= (const int &multi)
{
x = x * multi;
y = y * multi;
z = z * multi;
return *this;
}
The code runs:
int main()
{
Position p5( 1, 2, 3 );
p5.print();
Position p6( 4, 5, 6 );
p6.print();
Position p7 = p5 + p6;
p7.print();
p7 *= 2;
p7.print();
(p7 *= 2) *= 3;
p7.print();
}
The result yielded are:
1,2,3
4,5,6
5,7,9
10,14,18
20,28,36
Question is why won't the last result perform as it should when it is done in nested?
(p7 *= 2) *= 3;
won't work since you're assigning to a temporary, as you return by value in the operator*= function.
For the sub-expression
p7 *= 2
your operator function is called only for its side-effect, as the temporary it returns would be thrown away. To know more about temporaries, read What are C++ temporaries? and Temporary objects - when are they created, how do you recognise them in code?
When you want expression chaining, you've to return by reference like so
Position& Position::operator*= (int multi)
{
x = x * multi;
y = y * multi;
z = z * multi;
return *this;
}
Aside: You don't have to pass built-in types by const reference(const int &multi), non-const copy (int multi) should do fine.
Related
I receive the following error on declaring a vector of Point in the code shown (see reference to class template instantiation std::vector<Point,std::allocator<Point>> being compiled):
Severity Code Description Project File Line Suppression State
Error C2558 class 'Point': no copy constructor available or copy
constructor is declared 'explicit' AI assignment 1 C:\Program Files
(x86)\Microsoft Visual
Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xmemory 671
#include <iostream>
#include<vector>
using namespace std;
int dx, dy, sx, sy;
class Point
{
public:
int x;
int y;
float g;
float h;
Point()
{}
Point(int s, int d)
{
x = s;
y = d;
h = pow(pow(x - dx, 2) + pow(y - dy, 2), 1 / 2);
}
Point(Point &p)
{
x = p.x;
y = p.y;
g = p.g;
h = p.h;
}
bool operator == (Point p)
{
if (p.x == x && p.y == y)
return true;
else
return false;
}
};
class RoutePlaner
{
vector<vector<char>>grid;
int mapsize;
Point start;
Point destination;
int no_of_obstacles;
vector<Point> obstacles;
void generate_random_obstacles(int num)
{
for (int i = 0; i < num; i++)
{
int k = rand() % mapsize;
int j = rand() % mapsize;
Point p(i, j);
grid[j][k] = 'X';
obstacles.push_back(p);
}
}
public:
RoutePlaner(int m, Point s, Point d, int no)
{
mapsize = m;
start = s;
destination = d;
no_of_obstacles = no;
vector<char> vec(mapsize, '.');
for (int i = 0; i < mapsize; i++)
{
grid.push_back(vec);
}
//Setting start and destination
grid[start.x][start.y] = 'S';
grid[destination.x][destination.y] = 'D';
// setting obstacles
generate_random_obstacles(no_of_obstacles);
}
};
int main()
{
}
How should I declare a vector of class objects? How do I resolve the error?
You need your copy constructor for the Point class to conform to what std::vector (and many other aspects of the STL and C++ language) expects: that means its argument should be a const reference:
Point(const Point& p)
{
x = p.x;
y = p.y;
g = p.g;
h = p.h;
}
Although, in your case, as pointed out in the comment by M.M, the compiler-generated copy constructor will do exactly the same job as yours, so you can just omit your 'explicit' version. Furthermore, if you do declare your own copy constructor, you should also declare a destructor and an assignment operator, in order to follow to "Rule of Three." (Or you can 'meet halfway' by explicitly declaring the default copy constructor: Point(const Point& p) = default;.)
Note, also, that you can simplify your operator == function: whenever you have code of the form, if (x) return true; else return false; you should consider using just return (x);, like this:
bool operator == (Point p) {
return (p.x == x && p.y == y);
}
I have to use template because the requirement is that x, y, z can be of any types (int, float, double, long, etc).
#include <conio.h>
#include <iostream>
using namespace std;
template <class T>
class TVector //this class is for creating 3d vectors
{
T x, y, z;
public:
TVector() //default value for x, y, z
{
x = 0;
y = 0;
z = 0;
}
TVector(T a, T b, T c)
{
x = a; y = b; z = c;
}
void output()
{
cout << x << endl << y << endl << z;
}
//overloading operator + to calculate the sum of 2 vectors (2 objects)
TVector operator + (TVector vec)
{
TVector vec1;
vec1.x = this->x + vec.x;
vec1.y = this->y + vec.y;
vec1.z = this->z + vec.z;
return vec1;
}
};
int main()
{
TVector<int> v1(5, 1, 33);
TVector<float> v2(6.11, 6.1, 5.1);
TVector<float> v3;
v3 = v1 + v2;
v3.output();
system("pause");
return 0;
}
If the object v1 was float then the above code would run perfectly. However the requirement is that vector v1 has int as its data type. How do i solve this?
I already tried to use template for overloading + operator, my code looks like this:
template <typename U>
TVector operator+(TVector<U> vec)
{
TVector vec1;
vec1.x = this->x + vec.x;
vec1.y = this->y + vec.y;
vec1.z = this->z + vec.z;
return vec1;
};
^ Still doesn't work:
Your problem has nothing (or very little) to do with operator+ overloading. The compiler error says it all: v1 + v2 produces a vector of type TVector<int> (since that's how you defined the operator+), and you trying to assign it to v3 of type TVector<float>. But you haven't defined assignment operator for TVectors of different types (and this is exactly what compiler tells you in the error message)!
Beginner at C++ here. I specifically need help trying to figure out what is wrong with my overloaded * operator which is supposed to multiply two polynomials of a class Poly I made. My other overloaded operators appear to work just fine. Here is the original question:
P(x) = 2x4 + 3x3 – 12x2 + x – 19 (4th order polynomial)
or
P(x) = 2x7 + 5x5 – 7x2 + x – 19 (7th order polynomial)
Where the coefficients for the first and second equations can be described by the following array of integers
'Coeff1[ ] = {-19, 1, -12, 3, 2}'
'Coeff2[[ ] = {-19, 1, -7, 0, 0, 5, 0, 2}'
Design and code a polynomial class in C++ that has the following properties:
class Poly{
private:
int order; //order of the polynomial
int *coeff; // pointer to array of coeff on the heap
// size of coeff array predicated on (order + 1)
public:
Poly( ); //default constructor – order=0 & coeff[0] =1
Poly(int Order , int Default = 1) ;// creates Nth order poly
// and inits all coeffs
Poly(int Order, int *Coeff); //creates an Nth polynomial & inits
~Poly( ); // destructor
::::::: // copy constructor
//mutators & accessors
void set( ){// Query user for coefficient values);
void set(int coeff[ ], int size); // input coeffs via external coeff vector
int getOrder( )const; // get order of polynomial
int * get( ); //returns pointer to coeff array
//overloaded operators
Poly operator+( const Poly &rhs); // add two polynomials
Poly operator-( const Poly &rhs); // subt two polynomials
Poly operator*( const int scale); // scale a polynomial
Poly operator*(const Poly &rhs); // mult two polynomials
bool operator==(const Poly &rhs); // equality operator
const int & operator[ ](int I)const; // return the Ith coefficient
int & operator[ ](int I); // return the Ith coefficient
int operator( )(int X); // evaluate P(x) according
Poly & operator=(const Poly & rhs);
friend ostream & operator<<(ostream & Out, const Poly &rhs);
//other member functions
};
Demonstrate the following operations for the following Polynomials:
P1(x) = 2x4 + 3x3 – 12x2 + x – 19 (4th order polynomial)
P2(x) = 2x7 + 7x5 – 6x2 + x – 19 (7th order polynomial)
//display the following results for the polynomials defined above
o P3 = P1 + P2;
o P3 = P2 – P1;
o P3 = P1*10;
o P3 = 10*P1;
o P3 = P1*P2;
o bool flag = (P1==P2);
o P1[3] = P2[5]; // assign the 5th coefficient of P2 to 3rd coefficient of P1
o int Z = P1(int X = 5); // evaluate Polynomial for input X
// suggest using Horner’s method
o The displayed polynomial for P2 should be printed as follows
2X^7 + 7X^5 – 6X^2 + 1X – 1
Heres my code so far:
#include <iostream>
#include <cmath>
using namespace std;
class Poly
{
private:
int order;
int *coeff;
public:
Poly();
Poly(int Order, int Default=1);
Poly(int Order, int *Coeff);
Poly(const Poly ©);
~Poly();
void set(); //ask the user for the coefficient values
void set(int *Coeff, int size); //put the coefficient values in a external coeff vector
int getOrder() const; //gets the order of the polynomial
int* get() const; //returns pointer to coeff array
Poly operator +(const Poly &rhs);
Poly operator -(const Poly &rhs);
Poly operator *(const int scale);
Poly operator *(const Poly &rhs);
bool operator ==(const Poly &rhs);
const int & operator [](int access) const;
int & operator [](int access);
int operator ()(int X);
Poly & operator =(const Poly &rhs);
friend ostream & operator <<(ostream &Out, const Poly &rhs);
};
int main() {
int coeff1[] = {-19,1,-12,3,2};
int coeff2[] = {-19,1,-7,0,0,5,0,2};
Poly P1(4,coeff1);
Poly P2(7, coeff2);
Poly P3;
cout << "P1: " << P1 << endl;
cout << "P2: " << P2 << endl;
P3 = P1 * P2;
cout << "P1 * P2: " << P3 << endl;
return 0;
}
Poly::Poly() : order(0)
{
coeff = new int[1];
coeff[0] = 1;
}
Poly::Poly(int Order, int Default) : order(Order)
{
coeff = new int[order+1];
for (int i = 0; i < order+1; i++){
coeff[i] = Default;
}
}
Poly::Poly(int Order, int *Coeff) : order(Order), coeff(Coeff)
{
}
Poly::Poly(const Poly & copy)
{
order = copy.getOrder();
coeff = new int[order+1];
for (int i = 0; i < order+1; i++){
coeff[i] = copy.get()[i];
}
}
Poly::~Poly()
{
//if(coeff){
//delete [] coeff;
//}
}
void Poly::set()
{
cout << "Enter your coefficients:\n";
for (int i = 0; i < order+1; i++){
cin >> coeff[i];
}
}
void Poly::set(int *Coeff, int size)
{
delete [] coeff;
coeff = new int[size];
order = size;
for (int i = 0; i < order+1; i++){
coeff[i] = Coeff[i];
}
}
int Poly::getOrder() const
{
return order;
}
int* Poly::get() const
{
return coeff;
}
Poly Poly::operator +(const Poly &rhs)
{
int length = max(order+1, rhs.getOrder()+1);
int *answer = new int[length];
for (int i = 0; i < length + 1; i++){
answer[i] = coeff[i] + rhs.get()[i];
}
if (order > rhs.getOrder()){
for (int i = order+1 - rhs.getOrder()+1 ; i < order+1; i++){
answer[i] = coeff[i];
}
}
else if (order < rhs.getOrder()){
for (int i = rhs.getOrder()+1 - order+1; i < rhs.getOrder()+1; i++){
answer[i] = rhs.get()[i];
}
}
return Poly(length-1, answer);
}
Poly Poly::operator -(const Poly &rhs)
{
int length = max(order+1, rhs.getOrder()+1);
int *answer = new int[length];
for (int i = 0; i < order+1 && i < rhs.getOrder() + 1; i++){
answer[i] = coeff[i] - rhs.get()[i];
}
if (order > rhs.getOrder()){
for (int i = order+1-rhs.getOrder()+1; i < order+1; i++){
answer[i] = coeff[i];
}
}
else if (order < rhs.getOrder()){
for (int i = rhs.getOrder()+1 - order+1; i < rhs.getOrder()+1; i++){
answer[i] = 0 - rhs.get()[i];
}
}
return Poly(length-1, answer);
}
Poly Poly::operator *(const int scale)
{
int *answer = new int[order+1];
for (int i = 0; i < order+1; i++){
answer[i] = coeff[i] * scale;
}
return Poly(order, answer);
}
Poly Poly::operator *(const Poly &rhs)
{
int *shorter = NULL;
int *longer = NULL;
int s = 0;
int l = 0;
if(order < rhs.getOrder()){
shorter = coeff;
s = order;
longer = rhs.coeff;
l = rhs.order;
} else {
shorter = rhs.coeff;
s = rhs.order;
longer = coeff;
l = order;
}
Poly sum = Poly(l, longer) * shorter[0];
int *prod;
int nl;
for (int i = 1; i <= s; i++){
nl = l + i;
prod = new int[nl + 1];
for(int j = 0; j < i; j++){
prod[j] = 0;
}
for(int k = 0; k <= l; k++){
prod[k+i] = shorter[i] * longer[k];
}
sum = sum + Poly(nl, prod);
}
return sum;
}
bool Poly::operator ==(const Poly &rhs)
{
bool result;
if (order == rhs.order){
result = true;
for(int i = 0; i<order+1; i++){
if (coeff[i] != rhs.get()[i]){
result = false;
}
}
}else result = false;
return result;
}
const int& Poly::operator[](int access) const
{
return coeff[order + 1 - access];
}
int& Poly::operator [](int access)
{
return coeff[order + 1 - access];
}
int Poly::operator ()(int x)
{
int total = 0;
for(int i = 0; i < order + 1; i++){
total += coeff[i] * pow(x, i);
}
return total;
}
Poly &Poly::operator =(const Poly &rhs)
{
order = rhs.getOrder();
coeff = rhs.get();
return *this;
}
ostream& operator <<(ostream & Out, const Poly &rhs)
{
Out << rhs.get()[rhs.getOrder()] << "x^" << rhs.getOrder(); //first
for (int i = rhs.getOrder()-1; i > 0; i--){
if (rhs.get()[i] < 0 || rhs.get()[i] > 1) {
if(rhs.get()[i] > 0){
Out << " + ";
}
Out << rhs.get()[i] << "x^" << i << " ";
}else if (rhs.get()[i] == 1){
Out << "+ x ";
}else if (rhs.get()[i] == 1){
Out << "- x";
}
}
if (rhs.get()[rhs.getOrder() - rhs.getOrder()] > 0) {
Out << " + " << rhs.get()[rhs.getOrder() - rhs.getOrder()]; //last
}else Out << rhs.get()[rhs.getOrder() - rhs.getOrder()]; //last
Out << endl;
return Out;
}
`
Here is my current output. The answer I keep getting is half of the correct answer but I can't seem to get the first half.
P1: 2x^4 + 3x^3 -12x^2 + x -19
P2: 2x^7 + 5x^5 -7x^2 + x -19
P1 * P2: -114x^5 + 49x^4 -76x^3 + 362x^2 -38x^1 + 361
Any help is appreciated. Thank you
First, stop manually managing memory. Replace order and coeff with std::vector<int> values;. This bundles size() for order, and handles memory management for you.
If you don't know how to use std::vector yet, learn: it is far easier than learning how to write your own Poly class.
The next step in implementing Poly * Poly is to implement Poly& operator*=( Poly const& rhs );
The final step is friend Poly operator*( Poly lhs, Poly const& rhs ) { return std::move(lhs*=rhs); } There is little reason to use more than one line, and it can be inline.
That leaves operator*=.
The first step in implementing *= it so implement operator+(Poly const&, Poly const&), again via Poly& operator+=(Poly const&). As addition is easy, I will leave that to you.
The next step is scalar multiplication. Implement Poly& operator*=(int), and from it friend Poly operator*(Poly lhs, int x) { return std::move( lhs*=x ); } and friend Poly operator*(int x, Poly rhs) { return std::move( rhs*= x ); }. This is called 'scalar multiplication'.
Once we have those, *= becomes easy.
Store a copy of our initial value. (Poly init = std::move(*this);)
Create a return value (empty).
For each coefficient on the right hand side, do retval += coeff * init;
return *this = std::move(retval);
This is a sketch of the solution. To solve it really, you'll want to implement tests at each phase. Because I implemented *= in terms of other operations, testing each of those operations to make sure they work is key to having a debuggable *=. Then you test *=, then you test *, and then you are done.
If you are compiling in a compliant C++ compiler, a nice thing about using std::vector is that your default copy, move, assign and move-assign operations do the right thing, as does your default destructor. Seek to manage resources with specialized resource management classes, and follow The Rule of Zero, and you will have less pain.
Note that the above *= is not much easier to write than *, but in general *= is easier, so you should get into the habit anyhow.
Finally, note that this allocates more memory than is required, and is not optimal. It is, however, easy to get correct. After you have something like the above implemented, you can make it more optimal a number of ways. You can use karatsuba multiplication, you could use expression templates to avoid intermediate multiplication on the result += coeff * init; lines, you could reserve the right amount of space in result, or you could start playing with indexes manually.
The first step should be correctness, because if you have a correct algorithm you can at the very least use it to test your more optimal (trickier) algorithms.
Your code (for the polynomial multiplication) contains no comments and is rather opaque and hard to read/understand. So, I refuse to compile it (mentally) and can only guess, but it seems you don't know how multiplication of polynomials is defined, since your product polynomial is set to have order = min(order(A), order(B)) + 1, while correct is order(A)+order(B).
I suggest, you
1 make sure you understand polynomial multiplication before starting to code
2 write clear code with minimal instructions and useful comments (or better: useful variable names)
3 manage memory via the C++ standard library (using std::vector<>)
4 structure your code (write polynomial& polynomial::operator+=(polynomial const&) and then define the product as a standalone (can be a friend) via
polynomial operator*(polynomial const&a,polynomial const&b)
{
auto c=a;
c*=b;
return std::move(c);
}
though you may want to improve on this particular design (avoiding the re-allocation the is necessary in the above code in the c*=b operation).
I think this:
sum = sum + Poly(length, prod);
should be
sum = sum + Poly(length + i - 1, prod);
Also, the loop on i should stop at the shortest coeff array length.
Here's a modified version of the function:
Poly Poly::operator *(const Poly &rhs)
{
int *shorter = NULL;
int *longer = NULL;
int s = 0;
int l = 0;
if(order < rhs.order){
shorter = coeff;
s = order;
longer = rhs.coeff;
l = rhs.order;
} else {
shorter = rhs.coeff;
s = rhs.order;
longer = coeff;
l = order;
}
Poly sum = Poly(l, longer) * shorter[0];
int *prod;
int nl;
for (int i = 1; i <= s; i++){
nl = l + i;
prod = new int[nl + 1];
for(int j = 0; j < i; j++){
prod[j] = 0;
}
for(int k = 0; k <= l; k++){
prod[k+i] = shorter[i] * longer[k];
}
sum = sum + Poly(nl, prod);
}
return sum;
}
Note how it is based on order values rather than coeff array lengths (contrarily to the fix I indicated at the top of this answer).
If it doesn't work for you, then you may have other bugs in the code you didn't provide, or your algorithms work with array lengths, so you might have to adjust either to get things working.
Finally, as it has been said in other answers, you should use the tools provided by the standard library instead of handling array allocation by hand.
So here is my main function. I was just trying to test if the array of classes and their member functions worked (which they did not).
int main(void)
{
Circle locCircles[5]();
locCircles[0].setCircle(0.000597, 32.684114, -117.180610);
cout << locCircles[0] << endl;
cout << "Hello world!" << endl;
return 0;
}
And these are the classes.
class Point2d{
public:
Point2d() {}
Point2d(double x, double y)
: X(x), Y(y) {}
double x() const { return X; }
double y() const { return Y; }
/**
* Returns the norm of this vector.
* #return the norm
*/
double norm() const {
return sqrt( X * X + Y * Y );
}
void setCoords(double x, double y) {
X = x; Y = y;
}
// Print point
friend std::ostream& operator << ( std::ostream& s, const Point2d& p ) {
s << p.x() << " " << p.y();
return s;
}
private:
double X;
double Y;
};
class Circle{
public:
/**
* #param R - radius
* #param C - center
*/
Circle(double R, Point2d& C)
: r(R), c(C) {}
/**
* #param R - radius
* #param X - center's x coordinate
* #param Y - center's y coordinate
*/
Circle(double R, double X, double Y)
: r(R), c(X, Y) {}
void setCircle(double r, double x, double y) {
r = r; c.setCoords(x, y);
}
Point2d getC() const { return c; }
double getR() const { return r; }
size_t intersect(const Circle& C2, Point2d& i1, Point2d& i2) {
// distance between the centers
double d = Point2d(c.x() - C2.c.x(),
c.y() - C2.c.y()).norm();
// find number of solutions
if(d > r + C2.r) // circles are too far apart, no solution(s)
{
std::cout << "Circles are too far apart\n";
return 0;
}
else if(d == 0 && r == C2.r) // circles coincide
{
std::cout << "Circles coincide\n";
return 0;
}
// one circle contains the other
else if(d + min(r, C2.r) < max(r, C2.r))
{
std::cout << "One circle contains the other\n";
return 0;
}
else
{
double a = (r*r - C2.r*C2.r + d*d)/ (2.0*d);
double h = sqrt(r*r - a*a);
// find p2
Point2d p2( c.x() + (a * (C2.c.x() - c.x())) / d,
c.y() + (a * (C2.c.y() - c.y())) / d);
// find intersection points p3
i1.setCoords( p2.x() + (h * (C2.c.y() - c.y())/ d),
p2.y() - (h * (C2.c.x() - c.x())/ d)
);
i2.setCoords( p2.x() - (h * (C2.c.y() - c.y())/ d),
p2.y() + (h * (C2.c.x() - c.x())/ d)
);
if(d == r + C2.r)
return 1;
return 2;
}
}
// Print circle
friend std::ostream& operator << ( std::ostream& s, const Circle& C ) {
s << "Center: " << C.getC() << ", r = " << C.getR();
return s;
}
private:
// radius
double r;
// center
Point2d c;
};
I can't seem to get rid of the build errors:
132 error: declaration of 'locCircles' as array of functions
133 error: 'locCircles' was not declared in this scope
Does anyone have any advice? I have been fiddling and researching this for hours.
Thanks.
You need to define a default construct for your Circle class and remove parentheses from Circle locCircles[5](); (i.e., change to Circle locCircles[5];)
Live Demo
I've been trying to resolve a error for the last day or so and can't seem to work it through.
It's the kind of error that the fix is probably very easy :S
I tried to search for similar questions but the fixes don't apply.
main.c
int main(int argc, char *argv[]){
int width, height;
std::vector<Obj*> world;
world.push_back(new Sphere(Vec(0, 0, -22), 2, Vec(0.2, 0.2, 0.2), true));
(...)
return 0;
}
The error is found when I try to create a Sphere.
Relevant classes
Obj.h
class Obj
{
public:
Vec color;
bool culling;
virtual bool intersect(const Vec &ray_orig, Vec &ray_dir, float *t0 = NULL, float *t1 = NULL) = 0;
};
Sphere.h
class Sphere: public Obj
{
public:
Vec center;
float radius, radius2;
Sphere(Vec center, float radius, Vec color, bool culling);
bool intersect(const Vec &ray_orig, Vec &ray_dir, float *t0 = NULL, float *t1 = NULL);
};
Sphere.c
Sphere::Sphere(Vec center, float radius, Vec color, bool culling){
this->center = center;
this->radius = radius;
this->color = color;
this->culling = culling;
}
bool Sphere::intersect(const Vec &ray_orig, Vec &ray_dir, float *t0 = NULL, float *t1 = NULL) {...}
These second error appears when I do this->color = color;. Not sure if they are related.
Vec is a simple struct with 3 variables.
If you need more information I'll add as soon as possible.
Thank you in advance.
Jose
EDIT
Sorry for the delay.
intersect(...) is the only function in the obj class. Is there anyway to maintain the abstraction since there are several objects(sphere,box,...).
As requested here goes the definition of vec.h
vec.h
struct Vec {
float x, y, z;
Vec() {x = 0, y = 0, z = 0;}
Vec(float val) {x = val, y = val, z = val;}
Vec(float x_val,float y_val,float z_val) {x = x_val, y = y_val, z = z_val;}
Vec(const Vec& copy) : x(copy.x), y(copy.y), z(copy.z) { }
Vec operator+ (const Vec& p) const {
return Vec(x + p.x, y + p.y, z + p.z);
}
Vec operator- (const Vec& p) const {
return Vec(x - p.x, y - p.y, z - p.z);
}
Vec& operator += (const Vec& p) {
x += p.x; y += p.y; z += p.z;
return *this;
}
Vec& operator -= (const Vec& p) {
x -= p.x; y -= p.y; z -= p.z;
return *this;
}
Vec operator* (const float f) const {
return Vec(f * x, f * y, f * z);
}
Vec& operator*= (const float f) {
x *= f; y *= f; z *= f;
return *this;
}
Vec operator/ (const float f) const {
float inv = 1.f / f;
return Vec(inv * x, inv * y, inv * z);
}
Vec& operator/= (const float f) {
float inv = 1.f / f;
x *= inv; y *= inv; z *= inv;
return *this;
}
Vec& operator= (const Vec& p) {
x = p.x; y = p.y; z = p.z;
return *this;
}
bool operator== (const Vec& p) {
if(x == p.x && y == p.y && z == p.z)
return true;
return false;
}
float length_squared() const {
return x*x + y*y + z*z;
}
float length() const {
return sqrt(length_squared());
}
Vec norm() const {
float nor = x * x + y * y + z * z;
if (nor > 0) {
float invNor = 1 / sqrt(nor);
(float)x *= invNor, (float)y *= invNor, (float)z *= invNor;
}
return *this;
}
Vec cross(const Vec&b) {
return Vec(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);
}
float dot(const Vec& v) {
return x * v.x + y * v.y + z * v.z;
}
};
It seems the problem was indeed in the vec file.
Can't find a reasoning about the fix, but changing the Vec struct to a class fixed all the problems.
Thanks for the help.