0
votes

I am using omnet++ that follows c++, I want to make one simple controller. I want to change the value of static members of the controller from another file. But when I compile the code it generates undefined references.

My code is written following, Kindly suggest to me what should I do, Thanks in advance.

//controlTest.h
namespace OPTxGsPON {
class controlTest:public cSimpleModule, public cListener 
{
public:
    static int bbb;

protected:
    virtual void initialize();
};
}; //namespace
//controlTest.cc
#include "controlTest.h"
namespace OPTxGsPON {
void controlTest::initialize()
{
    controlTest::bbb = 0;
}
}; //namespace

//User.h
#include "controlTest.h"

namespace OPTxGsPON {
class User :public cSimpleModule
{
protected:
    virtual void initialize();
};
}; //namespace
//User.cc
#include "controlTest.h"
#include "User.h"

namespace OPTxGsPON {
void User::initialize()
{
     controlTest::bbb=12;
}
}; //namespace

Error: ../out/gcc-release/src/User/User.o:User.cc:(.rdata$.refptr._ZN9OPTxGsPON11controlTest2bbE[.refptr._ZN9OPTxGsPON11controlTest2bbE]+0x0): undefined reference to `OPTxGsPON::controlTest::bbb'

Please guide me How will I fix it...

1
You need to define the static class members -- you've only declared them. See this post.G.M.
Thank you very much for help but it gives the same error when I define members in ``` //controlTest.cc void controlTest::initialize() { controlTest::bbb = 0; } ```khalid
Please edit your question to provide a minimal reproducible example and state any error message(s) verbatim.G.M.
That is not how you initialize state members. View the bottom of the second answer in the duplicate post.ChrisMM

1 Answers

0
votes

Thank you JHBonarius, G.M and ChrisMM , All of your guidance. Again, I follow the last comment of ChrisMM, and again tried to search suitable solution and I got it. The following code is running well.

//controlTest.h
namespace OPTxGsPON {
class controlTest:public cSimpleModule, public cListener 
{
public:
   static inline int bbb; //using inline word but really I do not know what is inline meaning!

protected:
    virtual void initialize();
};
}; //namespace

//controlTest.cc
#include "controlTest.h"
namespace OPTxGsPON {
void controlTest::initialize()
{}
}; //namespace
//User.h
#include "controlTest.h"
namespace OPTxGsPON {
class User :public cSimpleModule
{
protected:
    virtual void initialize();
};
}; //namespace

//User.cc
#include "controlTest.h"
#include "User.h"

namespace OPTxGsPON {
void User::initialize()
{
    controlTest::bbb=12;
}
}; //namespace

//Server.h
#include "controlTest.h"
class Server :public cSimpleModule
{
protected:
    virtual void finish();
};
}//namespace

//Server.cc
#include "controlTest.h"
#include "Server.h"
namespace OPTxGsPON {
void Server::finish()
{
    std::cout<<"WoW! Server got value  = "<<controlTest::bbb<<endl;
}
}//namespace

The output is WoW! Server got value  = 12

Thank you very much ...