Thread: problem: passing `xx' as `this' argument of `int xx::accessor()' discards qualifiers

  1. #1
    Registered User
    Join Date
    Mar 2008

    Question problem: passing `xx' as `this' argument of `int xx::accessor()' discards qualifiers

    Hi, I wrote a simplified version of a program I was having a problem with, to better illustrate my questions:
    1. How do I bypass this error?
    2. What is the exact cause?
    #include <iostream>
    using namespace std;
    class Stuff {
                    Stuff(const Stuff&);
                    int accessor();
                    void mutator(int);
                    int mystery;
    Stuff::Stuff(): mystery(0) {}
    Stuff::Stuff(int b): mystery(b) {}
    Stuff::Stuff(const Stuff& b)
            mystery = b.accessor();
    int Stuff::accessor() { return mystery; }
    void Stuff:: mutator(int b) { mystery = b; }
    int main()
            Stuff fool;
            cout << fool.accessor() << endl;
            return 0;
    and the error that spits out:
    bash-3.00$ !g
    g++ test.c
    test.c: In copy constructor `Stuff::Stuff(const Stuff&)':
    test.c:20: error: passing `const Stuff' as `this' argument of `int Stuff::accessor()' discards qualifiers
    Thanks dudes.

  2. #2
    Registered User
    Join Date
    Jan 2005
    The accessor function should be const, since it does not modify the state of the instance it is called on.

    Of course, there's no real need for you to write your own copy constructor, the compiler generated one will work correctly. Even if you did (because the actual code is different), I would use the initializer list and access mystery directly.

    Regardless, you should make your accessor function const correct to fix the error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Command Line Argument problem
    By dredre in forum C Programming
    Replies: 2
    Last Post: 04-29-2004, 01:26 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. problem passing an option as command line argument
    By papous in forum C Programming
    Replies: 3
    Last Post: 11-22-2001, 06:12 PM