• 02-25-2009
EstateMatt
√ the book
√ 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```
• 02-25-2009
tabstop
There isn't really such a thing as "const void". You can have "void print() const", though, if you want to be able to call the member function on a const object.
• 02-25-2009
EstateMatt
Got it.

So, Const as prefix returns a const object, const as suffix works on a const this.
• 02-25-2009
Elysia
Const binds to the type if it's before the function because const T and T const are the same thing.
Therefore, const must come after the function name if the function itself should be const.
And remember that temporaries will only bind to const T, thus you won't be able to call non-const functions on temporaries (which your result is).
• 02-25-2009
anon
You return a const object (sum / 2 ) and then try to perform a non-const operation (print()) on it.

Actually print should be a const method and it actually doesn't modify the instance, so you just need to mark it as such:

Code:

`void print() const {cout<<"("<<x<<", "<<y<<", y"<<z<<")";}`
Now it would be possible to call print() on objects that are marked unmodifiable (const).

All in all, you cannot use the const keyword if you don't do it consistently.

Look at all member functions and determine whether they should or should not be callable on const objects.
• 02-25-2009
EstateMatt
Thanks guys. I appreciate this. I'll give myself a few more No-Duh questions and I should be off and running. I really wish they would teach languages in a context where they were useful out of the box. It would be a lot easier to learn