我正在尝试为应用程序编写框架(用于数据库连接池),但也希望能够将其编译为单独的模块。 我所拥有的:
#include <iostream>
#define OK 0
#define ERROR -1
//--- DUMMY DECLARATIONS BELOW TO MAKE IT COMPILE ---//
#define LOG_NS_ERROR std::cout
class test{
public:
int get_child(std::string x);};
test *_global;
typedef struct test_struct{} _db_transact;
class db_transact{
public:
db_transact(int,int&,int&);
};
int _ns;
int _log_id;
//--- DUMMY DECLARATIONS ABOVE TO MAKE IT COMPILE ---//
class db_c_hndlr {
public:
db_c_hndlr(unsigned int poolsz);
~db_c_hndlr(void);
_db_transact *db_conn_get(void);
private:
int pool_alloc(unsigned int poolsz);
_db_transact **_db_pool;
unsigned int _used;
unsigned int _poolsz;
};
//---------------------------------------------------------
db_c_hndlr::db_c_hndlr(unsigned int poolsz) {
if (!poolsz){
LOG_NS_ERROR << "A valid poolsize >0 must be specified";
return;
}
_poolsz = poolsz;
if(pool_alloc(poolsz)==OK)
_used = 0;
else
LOG_NS_ERROR << "db_c_hndlr::db_c_hndlr couldn't allocate poolsize " << poolsz;
}
//---------------------------------------------------------
db_c_hndlr::~db_c_hndlr() {
int i = 0;
for (i=0; i<_poolsz; i++) {
if (_db_pool[i])
if (nullptr != _db_pool[i]) delete _db_pool[i];
}
}
//---------------------------------------------------------
int db_c_hndlr::pool_alloc(unsigned int poolsz) {
*_db_pool=(_db_transact*)calloc(poolsz, sizeof(_db_transact*));
if(_db_pool[poolsz-1])
return OK;
else
return ERROR;
}
//---------------------------------------------------------
_db_transact *db_c_hndlr::db_conn_get(void) {
if (_used < _poolsz)
_db_pool[_used] = new db_transact(_global->get_child("db_config"), _ns, _log_id);
else
return NULL;
return _db_pool[_used++];
}
但是当我尝试编译它时,我得到了:
$ gcc --std=c++11 pseudo_code.cpp
pseudo_code.cpp: In member function ‘_db_transact* db_c_hndlr::db_conn_get()’:
pseudo_code.cpp:66:25: error: cannot convert ‘db_transact*’ to ‘_db_transact* {aka test_struct*}’ in assignment
_db_pool[_used] = new db_transact(_global->get_child("db_config"), _ns,
^
为什么我想知道这个错误?