如何解决在 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
非常感谢...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。