代码:
class RXJH_DLL_API CLock
{
CRITICAL_SECTION _crisection;
public:
CLock()
{
InitializeCriticalSection( &_crisection );
}
~CLock()
{
DeleteCriticalSection( &_crisection );
}
void lock()
{
EnterCriticalSection( &_crisection );
}
void unlock()
{
LeaveCriticalSection( &_crisection );
}
};
template <class particle>
class RXJH_DLL_API particle_allocator
{
enum
{
chunk_size =1024
};
typedef unsigned char byte;
struct particle_list
{
byte *_data;
particle_list *_next;
};
std::list<byte *> _chunks; //显示这里错误
particle_list* _free_list;
CLock _guard;
public:
particle_allocator()
{
_free_list = 0;
_used_particle_number = 0;
_free_particle_number = 0;
}
~particle_allocator()
{
for(std::list<byte *>::iterator iter = _chunks.begin();iter!=_chunks.end();++iter)
{
delete [] (*iter);
}
while(_free_list)
{
particle_list *temp = _free_list;
_free_list = _free_list->_next;
delete temp;
}
}
particle* alloc_particle()
{
_guard.lock();
byte *momory;
particle_list* ¤t_list = _free_list;
if(!current_list)
{
momory = new byte[chunk_size*sizeof(particle)];
_chunks.push_front(momory);
for(int i =0;i< chunk_size;i++,_free_particle_number++)
{
particle_list *newnode = new particle_list;
newnode->_data = momory + i*sizeof(particle);
newnode->_next = current_list;
current_list = newnode;
}
}
byte *redata;
particle_list *de_node = current_list;
redata = _free_list->_data;
current_list = current_list->_next;
++_used_particle_number;
--_free_particle_number;
delete de_node;
_guard.unlock();
return new(redata) particle;
}
void free_particle(void *p)
{
byte *free_block = reinterpret_cast<byte *>(p);
_guard.lock();
particle_list* ¤t_list = _free_list;
particle_list *newnode = new particle_list;
newnode->_data = free_block;
newnode->_next = current_list;
current_list = newnode;
--_used_particle_number;
++_free_particle_number;
_guard.unlock();
}
void getallocator()
{
using namespace std;
cout << "_used_particle_number is " << _used_particle_number << " _free_particle_number is "<< _free_particle_number << endl;
}
public:
size_t _used_particle_number;
size_t _free_particle_number;
};
错误提示:
warning C4251: “particle_allocator<particle>::_chunks”: class“std::list<_Ty>”需要有 dll 接口由 class“particle_allocator<particle>”的客户端使用
------解决思路----------------------
std类不存在哦
------解决思路----------------------
楼主,这只是个警告啊
------解决思路----------------------
这个问题是由于封装dll的时候std没有导出造成的,好像是因为编译器不知道你dll里面的stl和使用地方stl的(版本)区别造成的,虽然实际上他们是一样的!
可以不用去管它,或者你干脆把warning C4251给禁止掉