No match for operator=

Hi I'm a beginner C++ developer so sorry in advance if my question could be not very "intelligent". I post the code so it's easier to understand the problem.

Obj.h
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
class Obj : public QObject
{
  Q_OBJECT

 public:
   typedef void (*factionState)();
   struct Tran {
	factionState Action;
	unsigned int nextState;
    };
   
   void processAction(myState) 
   {
     Tran const*t = myarrayAction + myState
     *(t->action)();
     myState=t->nextState;
   }

 private:
     Tran const *myarrayAction; 
      unsigned int numStates;

protected:
    myObj;
    myState;    

 public:
    Obj (Tran *arrayAction,  const int nStates, const int i) {arrayAction=myarrayAction, numStates = nStates;};

   void doNothing(){printf ("Do NOthing called\n")};


Obj_1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Obj.h"

const int Obj_1=1
class Obj_1 : public Obj
{
   private:
   typedef enum {
	OffState,
	InitState,
	RunState,
  }state ;

  static sensor_c::Tran  myarrayAction[3];

  public:

    Obj_1() : Obj(myarrayAction, 3, Obj_1) {myState=OffState, myObj=Obj_1,init();};

  private:
    void init();
    void GoToInitState();
    void GoToRunState();
};


Obj_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void  Obj_1::init()
{
    myarrayAction[3] = {
 	{&sensor_c::doNothing, OffState},
	{&his_c::GoToInitState, InitState},
	{&his_c::GoToRunState, RunState},
    };
}
void  Obj_1::GoToInitState()
{
// code;
}
void  Obj_1::GoToRunState()
{
// code;
}


When I build the code I have this error:

no match for 'operator=' (operand types are 'Obj::Tran' and '<brace-enclosed initializer list>'). Someone knows which is the problem?
Thanks
Try getting rid of the '=' sign and instead just initialize it. Otherwise you'll have to include the initializer list header and overload operator= to perform the initialization properly.
Thanks for your reply, but sorry I don't understand how I could do what do you told me....What does mean include the initializer list header and overload operator= to perform the initialization properly?
Before doing that, just try changing (for example) you line 3-7 of Obj_1::init() to the following:
3
4
5
6
7
myarrayAction[3] {
    {&sensor_c::doNothing, OffState},
    {&his_c::GoToInitState, InitState},
    {&his_c::GoToRunState, RunState},
};


Otherwise, have a look at http://www.cplusplus.com/reference/initializer_list/initializer_list/
Sorry there is a my error in paste and copy, where is written sensor_c is intended Obj, while where is written his_c is intended Obj_1

I tried your advice but if I put the code below in Obj_1::init() without '=' I have sintax error..
1
2
3
4
5
myarrayAction[3] {
    {&Obj::doNothing, OffState},
    {&Obj_1GoToInitState, InitState},
    {&Obj_1::GoToRunState, RunState},
};


I have already read the link so I have tried to put the inizialitation in the declaration.. in Obj_1.h
1
2
3
4
5
6
7
8
9
10
11
12
13

........

  private:
    void init();
    void GoToInitState();
    void GoToRunState();

	Obj::Tran Obj_1::myarrayAction[3]{
	   {&Obj::doNothing, OffState},
          {&Obj_1::GoToInitState, InitState},
          {&Obj_1::GoToRunState, RunState},
};


But it doesn't work, I have 3 errors:

cannot convert 'void (Obj_1::*)()' to 'Obj::factionState {aka void (*)()}' in initialization Obj_1.h

cannot convert 'void (Obj::*)()' to 'Obj_1::factionState {aka void (*)()}' in initialization Obj_1.h

extra qualification 'Obj_1::' on member 'myarrayAction' [-fpermissive] Obj_1.h

Last edited on
Topic archived. No new replies allowed.