在 c++/omnet++

问题描述

我使用的是遵循 c++ 的 omnet++,我想制作一个简单的控制器。 我想从另一个文件更改控制器的静态成员的值。但是当我编译代码时,它会生成未定义的引用。

我的代码如下编写,请建议我该怎么做,提前致谢。

//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

错误 ../out/gcc-release/src/User/User.o:User.cc:(.rdata$.refptr._ZN9OPTxGsPON11controlTest2bbE[.refptr._ZN9OPTxGsPON11controlTest2bbE]+0x0):对`OPTxGsPON::controlTest::bbb的未定义引用'

请指导我如何修复它...

解决方法

感谢 JHBonarius、G.M 和 ChrisMM,感谢您的指导。 再次,我遵循 ChrisMM 的最后一条评论,并再次尝试搜索合适的解决方案,我得到了它。以下代码运行良好。

//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

非常感谢...