Class error: assignment of data member in read-only structure

Hello!
I have this code, from the C++ in Action book.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
using namespace std;
class Input
{
    public:
    Input()
    {
        cout<<"\nThe input was created successfully";

    }
};
class IStack {};
class Calculator
{
    public:
    Calculator(): _done(false)
    {
        cout<<"\nCalculator created!";
    }
    bool Execute(Input & input)
    {
        cout<<"\nCalculator::Execute";
        return (!_done);
    }
    IStack const & GetStack() const
    {
        _done = true;
        return _stack;
    }
    private:
    IStack _stack;
    bool _done;

};

class StackSeq
{
    public:
    StackSeq(IStack const & stack): _stack(stack), _done(false)
    {
        cout<<"\nStack sequencer created succesfully.<><>";
    }
    bool AtEnd() const { return(_done); }
    void Advance() {_done = true; }
    int GetNum() const {return(13);}
    private:
    IStack const & _stack;
    bool _done;
};


int main()
{
    Calculator TheCalculator;
    bool status;
    do
    {    cout<<">";
         Input input;
         status = TheCalculator.Execute(input);
         if(status)
         {
             for(StackSeq seq(TheCalculator.GetStack()); !seq.AtEnd(); seq.Advance())
             {
                 cout<<" "<<seq.GetNum()<<"\n";
             }
         }

    }while(status);
    return 0;

}


I have an error which sounds like that:

error: assignment of data-member 'Calculator::_done' in read-only structure

What can I do with this? Please help me.

I compile with GNU GCC, C::B IDE.
Last edited on
IStack const & GetStack() const
This function has const after the parameter list (which happens to be empty - it's that const on the end). This is a promise that the function will not change any of the class variables.

_done = true; is an attempt to change one of the class variables. You broke a promise to the compiler, and the compiler isn't happy about it :(

Either don't make that promise, or don't try to break that promise.
Last edited on
Allright, I deleted that const at the end and I get then 10 more errors, like:

39: multiple definition of 'StackSeq(IStack const&)
42: first defined here
43: multiple definition of 'StackSeq::AtEnd() const

and so on. What can I do now?
Thank you.
When I compile this, it compiles without error. All I did was remove the const.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
using namespace std;
class Input
{
    public:
    Input()
    {
        cout<<"\nThe input was created successfully";

    }
};
class IStack {};
class Calculator
{
    public:
    Calculator(): _done(false)
    {
        cout<<"\nCalculator created!";
    }
    bool Execute(Input & input)
    {
        cout<<"\nCalculator::Execute";
        return (!_done);
    }
    IStack const & GetStack()
    {
        _done = true;
        return _stack;
    }
    private:
    IStack _stack;
    bool _done;

};

class StackSeq
{
    public:
    StackSeq(IStack const & stack): _stack(stack), _done(false)
    {
        cout<<"\nStack sequencer created succesfully.<><>";
    }
    bool AtEnd() const { return(_done); }
    void Advance() {_done = true; }
    int GetNum() const {return(13);}
    private:
    IStack const & _stack;
    bool _done;
};


int main()
{
    Calculator TheCalculator;
    bool status;
    do
    {    cout<<">";
         Input input;
         status = TheCalculator.Execute(input);
         if(status)
         {
             for(StackSeq seq(TheCalculator.GetStack()); !seq.AtEnd(); seq.Advance())
             {
                 cout<<" "<<seq.GetNum()<<"\n";
             }
         }

    }while(status);
    return 0;

}
Still the same errors.

I will reinstall the OS (Windows xp sp3) because I have a lot of unsafety stuff around here and might be a system reason of those errors. I will come back with news after I reinstall the OS.
Thanks for the help untill now.
I suspect you've screwed up the project setting on your IDE and are feeding multiple copies of the same thing to the linker.
Last edited on
I fixed it up. I reinstalled the OS and it compiled succesfully. Thanks a lot Moschops for your time.
Topic archived. No new replies allowed.