views:

140

answers:

2

My problem is that in my "Widget" class i've the following declaration:

MouseEvent* X;

In a member function I initialize the pointer with an address the normal way:

X = new MouseEvent;

Ok, this last line makes the compiler stop at:

error C2166: l-value specifies const object

All right, a MouseEvent is declared as a typedef to simplify things:

typedef Event__2<void, Widget&, const MouseEventArgs&> MouseEvent;

And Event__2 is, as you may imagine as: (basic structure shown):

template <typename return_type, typename arg1_T, typename arg2_T>
class Event__2
{
     ...
};

I don't know where the Event__2 class gets the const qualifier. Any tips ?

Thanks.

+3  A: 

Likely, the member function where you are initializing X is marked as const - something like this.

class Foo
{
   int *Bar;

public:

   void AssignAndDoStuff() const
   {
      Bar = new int; // Can't assign to a const object.
      // other code
   }
}

The solution here is either to

  1. Assign to Bar in a separate non-const method,
  2. change AssignAndDoStuff to be non-const, or
  3. mark Bar as mutable.

Pick one of the above:

class Foo
{
   mutable int *Bar; // 3

public:
   void Assign() // 1
   {
       Bar = new int; 
   }   
   void DoStuff() const
   {
       // Other code
   }

   void AssignAndDoStuff() // 2
   {
      Bar = new int; 
      // other code
   }
}
Eclipse
+1 for psychic debugging.
Greg Hewgill
Agreed. Impressive, Josh!
Shmoopty
A: 

Likely, the member function where you are initializing X is marked as const.

AAAARRRGHH!!! That was the problem.... Thank you Josh, this is caused by coding at 4:10 AM, i'm going now to bed :)

Thank you.

Hernán
Glad I can help - I've hit the same problem often enough!
Eclipse