user3481693
user3481693

Reputation: 191

No match for operator '='

Hi I'm a beginner C++ developer..I have a problem with a code that I post so it's easier to understand the problem.

Obj.h

    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

    #include "Obj.h"

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

  static Obj::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

    void  Obj_1::init()
{
    myarrayAction[3] = {
    {&Obj::doNothing, OffState},
    {&Obj_1::GoToInitState, InitState},
    {&Obj_1::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 ''). So I tried to remove '=' and write Obj1::init like this

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

But I have a sintax error..Any ideas?? Thanks

Upvotes: 2

Views: 271

Answers (1)

David K
David K

Reputation: 3132

A couple of things. First, when Obj_1::init() executes, myarrayAction has already been initialized by the constructor. The { } syntax, which works for some array initializations, is not going to work here. You need an object (not a list of initializers) on the right-hand side of =.

Second, it looks like you want Obj_1::init() to set the contents of all three of the Obj::Tran objects within myarrayAction. But when you start a statement like this, myarrayAction[3] =, it is going to try to set the array member myarrayAction[3], that is, the fourth member of the array myarrayAction (whose first three members are myarrayAction[0], myarrayAction[1], myarrayAction[2]). But this array has only three members.

You are better off to write something like this:

myarrayAction[0] = ... ;
myarrayAction[1] = ... ;
myarrayAction[2] = ... ;

In the ... parts you call the constructors of the three objects you want to store at those three locations, passing the values you want to the parameters of each of these constructors.

Another thing you could do is, instead of using Obj::Tran* to represent an array of Obj::Tran, actually use a class to implement this array. If you know STL you could write std::vector<Obj::Tran>, for example. Or you could write your own class, depending on what you want. If you write a class, you can also write a constructor that takes the list of values you want as parameters, though you will not be able to organize them into sublists with nested { } as you could with an array initializer list.

Upvotes: 2

Related Questions