【发布时间】:2009-11-21 06:00:43
【问题描述】:
我想要一个单例类,它的对象不是静态创建的。具有以下代码,当我调用 ChromosomePool::createPool() 时,我收到以下错误消息:
--> ChromosomePool.h:50: 未定义对 `myga::ChromosomePool::pool' 的引用
谁能告诉我如何解决这个问题?
class ChromosomePool {
private:
double * array;
const int len;
const int poolSize;
const int chromosomeLength;
static ChromosomePool* pool;
ChromosomePool(int size_of_pool, int length_of_chromosom):
poolSize(size_of_pool) , chromosomeLength(length_of_chromosom),
len(size_of_pool*length_of_chromosom){
array = new double[len];
}
public:
static void createPool(int size_of_pool, int length_of_chromosom) {
if(pool) {
DUMP("pool has already been initialized.");
exit(5);
}
pool = new ChromosomePool(size_of_pool,length_of_chromosom);
}
static void disposePool() {
if( !pool ) {
DUMP("Nothing to dispose");
exit(5);
}
pool ->~ChromosomePool();
pool = NULL;
}
static ChromosomePool* instace() {
if( !pool ) {
DUMP("Using pool before it is initialized");
exit(5);
}
return pool;
}
~ChromosomePool() {
delete[] array;
}
};
【问题讨论】:
标签: c++ design-patterns singleton compiler-errors