0

I am doing a network simulation using Omnet++ which is based on C++. I am trying to define a class for a certain module using the following code:

#ifndef __PROJECT_IMS_SLF_H_
#define __PROJECT_IMS_SLF_H_

#include <omnetpp.h>
#include "mDIAMETER_m.h"
#include "IPPacket_m.h"

class SLF : public cSimpleModule
{
  public:
    mDIAMETER *generateDIAMETERmsg(const char* name, long userID, bool registered, std::string server);
    IPPacket *generateIPPacket(int srcIP,int desIP);
  protected:
    virtual void initialize();
    virtual void handleMessage(cMessage *msg);
  private:
    int MyIP;
    int N = par("N");
    struct Registry {
            long UserID;
            bool Registered;
            std::string Server;
        };
    struct Registry MyReg[N];   // Create a Registry Table for all UEs

};



#endif

I am getting the following error: "invalid use of non-static data member SLF::N". I know I'm declaring N inside the class definition as a private variable but I have to do so because N is a parameter related to the module which is defined as an instance of the class SLF and it is read from the configurations of this module using the OMNET++ defined function "par".

If I declareMyReg[N]inside the initialize function it works but the problem is that I need to use this variable inside the other function (handleMessage). Any hints?

Thanks in advance!

3
  • Instead of using a C-style array, use a std::vector, then you don't need the N member variable as you can initialize the vector to the correct size in the constructor. Commented Feb 5, 2015 at 8:49
  • Standard C++ requires that the dimension be a constant. 'N' isn't a constant, and unless 'par("N")' is a constexpr function then I'm not sure it can ever be. Use a std::vector instead. Commented Feb 5, 2015 at 8:50
  • par("N") looks like a function call (although not shown in your question) that returns the value at runtime. The compiler needs to value at compile time when you declare MyReg[N]'. That's the reason you need to allocate the Registry objects meomry at runtime. Commented Feb 5, 2015 at 9:32

2 Answers 2

1

Allocate the array on the heap:

class SLF /* etc. */
{
    /* etc. */

    virtual void initialize()
    {
        /* etc. */

        N = par("N");
        MyReg = new Registry[N];

        /* etc. */
    }

    /* etc. */

    int N;

    /* etc. */

    struct Registry *MyReg;
}

And be sure to delete the memory in a destructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Or use a std::vector, but my answer was meant to keep the spirit of your original code.
0

Problem solved as follows:

  • First, define a pointer to the instance of this structure:

struct Registry *MyReg;

Now inside the module, within the initialize function, N can be read and the instance of the structure can be declared with its size:

int N = par("N");
this->MyReg = new struct Registry[N];    

I also defined a finish module withtin the header file and deleted MyReg inside that function.

2 Comments

That's exactly what Jim Buck stated in his answer :|
After I posted the answer I noticed Jims

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.