Thread: error: passing [...] discards qualifiers

  1. #1
    coder
    Join Date
    Feb 2008
    Posts
    127

    error: passing [...] discards qualifiers

    hello
    I'm learning how to use operators:
    this code compiles and works fine.
    Code:
    struct Item {
    	int data;
    	
    	Item  (const int value = 0) { data = value; }
    
    	Item operator+ (const Item &item) {
    		Item result (data + item.data);
    		return result;
    	}
    	Item operator* (const Item &item) {
    		Item result (data * item.data);
    		return result;
    	}
    };
    But if I want to make data private...
    Code:
    class Item {
    private:
    	int data;
    public:
    	Item  (const int value = 0) { data = value; }
    	int & Data () { return data; }
    
    	Item operator+ (const Item &item) {
    		Item result (data + item.Data ());
    		return result;
    	}
    	Item operator* (const Item &item) {
    		Item result (data * item.Data ());
    		return result;
    	}
    };
    
    int main () {
    	Item a (3);
    	Item b (4);
    	Item c;
    	c = a + b * a;
    
    	return 0;
    }
    I get:
    error: passing ‘const Item’ as ‘this’ argument of ‘int& Item::Data()’ discards qualifiers

    what does it mean?
    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Provide a const version of Data(), e.g.,
    Code:
    int Data () const { return data; }
    Also, member operator+ and operator* should be const. You can make use of return value optimisation with return Item(data + item.Data()), though perhaps named return value optimisation might still be applied.

    EDIT:
    Also, use the constructor initialisation list:
    Code:
    Item(int value = 0) : data(value) {}
    Last edited by laserlight; 03-19-2008 at 08:37 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    coder
    Join Date
    Feb 2008
    Posts
    127
    thanks laserlight, it works fine now.
    Although I missed the fact that I don't really need to use the function Data() inside the class, I just could read data directly.

    May you explain what "discard qualifiers" would mean exactly? I couldn't get a good idea from stuff found in the web.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    May you explain what "discard qualifiers" would mean exactly?
    item was passed by const reference. But item.Data() returns a reference (i.e., the const qualifier is discarded). Suppose this was allowed. Then, using the reference returned by item.Data(), you could modify item's contents, even though item is const. So discarding the const qualifier here is treated as an error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    coder
    Join Date
    Feb 2008
    Posts
    127
    ok, great
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. discards qualifiers
    By EstateMatt in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2009, 03:05 PM
  2. Replies: 8
    Last Post: 05-29-2008, 12:47 PM
  3. Replies: 1
    Last Post: 03-31-2008, 05:53 PM
  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