Can't figure out what's wrong with this code

Discussion in 'C++' started by Taras_96, Jan 24, 2008.

  1. Taras_96

    Taras_96 Guest

    Hi everyone,

    I can't figure out why g++ complains about this code. I gave it to a
    programmer friend and he couldn't work it out either. It's quite
    simple code, so it's puzzling me. The error I get is:

    ==========================================

    copy_constructor.cpp: In copy constructor `MyCounter::MyCounter(const
    MyCounter&)':
    copy_constructor.cpp:23: error: passing `const MyCounter' as `this'
    argument of `const int MyCounter::getValue()' discards qualifiers

    The code is:
    ==========================================

    #include <iostream>

    using namespace std;

    class MyCounter
    {
    public:
    MyCounter();
    ~MyCounter();
    MyCounter(const MyCounter &);
    const int getValue() {return itsVal;}
    private:
    int itsVal;
    };

    MyCounter::MyCounter():
    itsVal(0)
    {
    }

    MyCounter::MyCounter(const MyCounter & toCopy)
    {
    itsVal = toCopy.getValue();
    }

    MyCounter::~MyCounter()
    {
    }

    ===================================

    If I write 'toCopy.itsValue' instead, the code compiles

    Any help would be appreciated

    Thanks

    Taras
     
    Taras_96, Jan 24, 2008
    #1
    1. Advertising

  2. Taras_96

    Ian Collins Guest

    Taras_96 wrote:
    > Hi everyone,
    >
    > I can't figure out why g++ complains about this code. I gave it to a
    > programmer friend and he couldn't work it out either.


    Can't be a C++ programmer then!

    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class MyCounter
    > {
    > public:
    > MyCounter();
    > ~MyCounter();
    > MyCounter(const MyCounter &);
    > const int getValue() {return itsVal;}
    > private:
    > int itsVal;
    > };
    >
    > MyCounter::MyCounter():
    > itsVal(0)
    > {
    > }
    >
    > MyCounter::MyCounter(const MyCounter & toCopy)
    > {
    > itsVal = toCopy.getValue();


    getValue() isn't a const member function, so you can't call it on a
    const object.

    --
    Ian Collins.
     
    Ian Collins, Jan 24, 2008
    #2
    1. Advertising

  3. Taras_96

    Guest

    On Jan 24, 2:18 pm, Ian Collins <> wrote:
    > Taras_96 wrote:
    > > Hi everyone,

    >
    > > I can't figure out why g++ complains about this code. I gave it to a
    > > programmer friend and he couldn't work it out either.

    >
    > Can't be a C++ programmer then!
    >
    >
    >
    >
    >
    > > #include <iostream>

    >
    > > using namespace std;

    >
    > > class MyCounter
    > > {
    > > public:
    > > MyCounter();
    > > ~MyCounter();
    > > MyCounter(const MyCounter &);
    > > const int getValue() {return itsVal;}
    > > private:
    > > int itsVal;
    > > };

    >
    > > MyCounter::MyCounter():
    > > itsVal(0)
    > > {
    > > }

    >
    > > MyCounter::MyCounter(const MyCounter & toCopy)
    > > {
    > > itsVal = toCopy.getValue();

    >
    > getValue() isn't a const member function, so you can't call it on a
    > const object.
    >
    > --
    > Ian Collins.


    Instead of writing
    const int getValue() {return itsVal;}
    write
    const int getValue() const { return itsVal;}

    Thanks,
    Suman Nandan
     
    , Jan 24, 2008
    #3
  4. Taras_96

    Taras_96 Guest

    Thanks Ian and Suman, that was the problem. I'll make sure I give my
    friend this link :D
     
    Taras_96, Jan 29, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne  Wengert

    Can't Figure Out What is Wrong

    Wayne Wengert, Feb 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    450
    Michael H
    Feb 28, 2005
  2. Steve
    Replies:
    0
    Views:
    530
    Steve
    Sep 25, 2006
  3. Sam Roberts
    Replies:
    2
    Views:
    179
    Brett Williams
    Apr 14, 2004
  4. erik blas

    can't figure out what's wrong

    erik blas, Dec 27, 2005, in forum: Ruby
    Replies:
    0
    Views:
    97
    erik blas
    Dec 27, 2005
  5. erik blas

    can't figure out what's wrong

    erik blas, Dec 27, 2005, in forum: Ruby
    Replies:
    3
    Views:
    109
    erik blas
    Dec 27, 2005
Loading...

Share This Page