√ the book
√ google search
√ searched cboard
This is homework but I think it's a small enough and valid enough question.
I'm down to one error which is commented out in main.cpp
I've researched the "discards qualifiers error" and most solutions seem to say that it means I'm mismatching a const function with a non const object. as far as I can tell, operator/ returns const vec and print() is a const void.
any ideas?
one more question. My teacher says that operator overloading always carries the same constellation of modifiers. is there a reference somewhere that I can look up what I should be using for each one?
main.cpp
Code:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "vec.h"
using namespace std;
int main()
{
vec x(1, 0, 0);
vec y(0, 1, 0);
vec z(0, 0, 1);
vec sum = x + y;
cout << "The sum of x and y is ";
sum.print();
cout << ".\n";
cout << "The midpoint of x and y is ";
(sum / 2).print(); //division has lower precedence than dot
/*
error: passing 'const vec' as 'this' argument of 'const void vec::print()' discards qualifiers
*/
cout << ".\n";
double dot = x * y;
cout << "The dot product of x and y is " << dot << ".\n";
double theta = acos(dot / (x.length() * y.length())); //in radians
const double pi = 4 * atan2(1, 1);
cout << "The angle between x and y is "
<< theta / pi << "pi radians or "
<< theta * 180 / pi << " degrees.\n";
vec perpendicular = x ^ y;
cout << "A vector perpendicular to x and y is ";
perpendicular.print();
cout << ".\n";
if ((x ^ y) != z) { //^ has lower precedence than !=
cerr << "Error: should have had x ^ y == z.\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
vec.h
Code:
/*
* vec.h
* 3point6b
*
* Created by puuukeey on 2/24/09.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/
#ifndef VECH
#define VECH
#include <iostream>
#include <cmath>
using namespace std;
class vec{
double x;
double y;
double z;
public:
vec(double x =0,double y =0, double z =0);
const void print(){cout<<"("<<x<<", "<<y<<", y"<<z<<")";};
const double length() {return sqrt(pow(x,2)+pow(y,2)+pow(z,2) );};
friend double operator*(vec& v1,vec& v2){return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z ;};
friend bool operator==(const vec& v1, const vec& v2){return v1.x==v2.x&&v1.y==v2.y&&v1.z==v2.z; };
vec& operator+=(vec v2) {x += v2.x;y += v2.y;z += v2.z; return *this;}
vec& operator*=(double d2) {x *= d2;y *= d2;z *= d2; return *this;}
vec& operator/=(double d2) {x /= d2;y /= d2;z /= d2; return *this;}
vec& operator-=(vec v2) {x -= v2.x;y -= v2.y;z -= v2.z; return *this;}
friend vec operator^(vec& v1,vec& v2){return vec(v1.y*v2.z - v1.z*v2.y,v1.z*v2.x - v1.x*v2.z,v1.x*v2.y - v1.y*v2.x);};
};
inline bool operator!=(const vec& v1, const vec& v2) {return !(v1 == v2);}
inline const vec operator+( vec v1, vec v2) {return v1 += v2;}
inline const vec operator-(vec v1, vec v2) {return v1 -= v2;}
inline const vec operator- (vec negMe) { return vec(0-negMe); }
inline const vec operator*(vec v, double d) {return v *= d;}
inline const vec operator*(double d, vec v) {return v *= d;}
inline const vec operator/( vec v, double d) {return v /= d;}
#endif