我想要使用boost :: mutex功能add我的结构单元。这里是Detector.h未定义的参考boost ::互斥结构
class Detector {
private:
struct Cell{
static boost::mutex mutex_;
double energy;
double sqrt_energy;
int histories;
inline Cell(): energy(0.f), sqrt_energy(0.f), histories(0.f) {
boost::mutex mutex_; //I tried with and without this line
};
inline void add(double new_energy) {
mutex_.lock();
energy += new_energy;
sqrt_energy += new_energy*new_energy;
++histories;
mutex_.unlock();
};
};
typedef std::vector<Cell> CellVector;
CellVector EnergyPrimary;
}
在我的结构的定义,我用我的功能在Detector.cpp添加上细胞的载体。
Dectetor::Detector() : {
nVoxelX=1024;
nVoxelY=1024;
size=nVoxelX*nVoxelY;
EnergyPrimary=CellVector(size);
}
void Detector::Score(int cellID, double new_energy) {
EnergyPrimary[cellID].add(new_energy);
}
当我尝试编译它,我有mutex_.lock()和mutex_.unlock一个未定义的引用错误()。但是为什么当我使用类似的函数(当我调用EnergyPrimary [cellID] .energy + = new_energy;)重载operator + =时,它会起作用?
inline bool operator+= (double new_energy) {
mutex_.lock();
energy += new_energy;
mutex_.unlock();
return false;
};
你是否包含头文件?你的编译器知道在哪里找到提升? – Jepessen
您的'mutex_'是静态的,但我没有看到您定义它的位置。你真的只想要所有单元和所有检测器只有一个互斥体? – qPCR4vir
可能重复[什么是未定义的引用/未解析的外部符号错误,以及如何解决它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-符号错误和怎么办我修复) – Danh