Thread: discards qualifiers

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    discards qualifiers

    √ 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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Got it.

    So, Const as prefix returns a const object, const as suffix works on a const this.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-29-2008, 12:47 PM
  2. Replies: 1
    Last Post: 03-31-2008, 05:53 PM
  3. error: passing [...] discards qualifiers
    By carlorfeo in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 08:45 AM
  4. Replies: 3
    Last Post: 06-19-2004, 10:24 AM
  5. passing discards qualifiers? overloading =...
    By Captain Penguin in forum C++ Programming
    Replies: 9
    Last Post: 10-07-2002, 05:38 PM