error: passing [...] discards qualifiers

This is a discussion on error: passing [...] discards qualifiers within the C++ Programming forums, part of the General Programming Boards category; hello I'm learning how to use operators: this code compiles and works fine. Code: struct Item { int data; Item ...

  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
    22,266
    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 09:37 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,266
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01:47 PM
  3. Replies: 1
    Last Post: 03-31-2008, 06:53 PM
  4. Replies: 3
    Last Post: 06-19-2004, 11:24 AM
  5. passing discards qualifiers? overloading =...
    By Captain Penguin in forum C++ Programming
    Replies: 9
    Last Post: 10-07-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21