初始化对象时c++编译错误

c++ compile error when initializating an object

本文关键字:编译 错误 c++ 对象 初始化      更新时间:2023-10-16

我是c++新手,需要帮助解决一个错误。下面的代码在旧的Sun版本(CC 5.3和5.8)上可以很好地编译。我在发生错误的行上加了注释。非常感谢任何协助。非常感谢。-

Unix版本:SunOS ut51278 5.10 Generic_141444-09 sun4u sparc SUNW, sparc - enterprise
编译版本:CC: Sun c++ 5.11 sunnos_sparc 2010/08/13

database.h:

#include <dbserver.h> 
#include <dbstoredproc.h> 
#include "dbsql.h" 
class Database : public DBServer 
{ 
        public : 
                DBSql SQL( const char* sqlCmd ); 
                DBSql SQL( const std::string& sqlCmd ); 
                DBSql Table( const char* tableName );                         
                DBSql Table( const std::string& tableName ); 
                DBStoredProc storedProc( const char* spName ); 
                DBStoredProc storedProc( const std::string& tableName ); 
}; 

dbsql.h:

 #include <string> 
#include "dbaccess.h" 
#include "dbtable.h" 
class DBSql : public DBAccess 
{ 
public: 
        DBSql(DBServer *pServer,const char *sql) ; 
        DBSql(DBServer *pServer,const std::string& sql); 
        DBSql(DBSql& dbSql); 
        DBSql& operator=(DBSql& dbSql); 
        virtual DBTable getResultSet(); 
protected: 
        DBSql(); 
        void init(DBServer *pServer,const std::string& sql); 
}; 

database.cpp…见错误信息....

database.cpp:

#include <database.h> 
#include "dbsql.h" 
using namespace std; 
DBSql Database::Table( const char* tableName ) 
{ 
     return Table( string( tableName ) );  // Error: "Cannot use DBSql to initialize DBSql."
} 
DBSql Database::Table( const string& tableName ) 
{ 
      string sqlCmd = "select * from " + tableName; 
      return SQL( sqlCmd.c_str() );   
} 
DBSql Database::SQL( const char* sqlCmd ) 
{ 
      return DBSql(this,sqlCmd);           
} 
DBSql Database::SQL( const string& sqlCmd ) 
{ 
      return SQL( sqlCmd.c_str() );        
} 
DBStoredProc Database::storedProc( const char* spName ) 
{ 
     return DBStoredProc( this, spName ); 
} 
DBStoredProc Database::storedProc( const std::string& spName ) 
{ 
    return DBStoredProc( this, spName ); 
} 

dbsql.cpp:

#include "dbsql.h" 
#include "dbcommon.h" 
using namespace std; 
using namespace ORACLE; 
DBSql::DBSql(DBServer *pServer,const char* sql) 
{ 
   init(pServer,string(sql)); 
} 
DBSql::DBSql(DBServer *pServer,const string& sql) 
{ 
   init(pServer,sql); 
} 
DBSql::DBSql(DBSql& dbSql) 
 : DBAccess(dbSql) 
{ 
} 
DBSql& DBSql::operator=(DBSql& dbSql) 
{ 
  DBAccess::assign(dbSql); 
  return *this; 
} 
DBSql::DBSql() 
{ 
} 
void DBSql::init(DBServer *pServer,const string& sql) 
{ 
   execSQL(pServer,sql.c_str()); 
} 
DBTable DBSql::getResultSet() 
{ 
DBTable result; 
  if(DBAccess::getResultSet(false)) 
  { 
  //In order to prevent DBAccess from closing the previous result set, pass false to 
  //getResultSet 
    result = DBTable(m_pStmt,m_pResultSet,true); // Pass true to DBTable to allow it       
    m_pStmt = NULL;                              //  to control the statement. 
  } 
  m_pResultSet = NULL; 
  return result; 
} 

变化:

DBSql(DBSql& dbSql); 

:

DBSql(DBSql const & dbSql); 

调用Table( string( tableName ) )产生一个需要使用复制构造函数复制的临时对象,以便返回。由于该值是临时的,因此只能获得对它的const引用,而复制构造函数不接受这种引用。

因为你的复制构造函数和赋值操作符实际上并没有做任何的事情,你也可以把它们省略。如果您保留它们,您也应该将DBSql& operator=(DBSql& dbSql);更改为接受const引用- DBSql& operator=(DBSql const & dbSql); -并在父类复制构造函数和赋值操作符中进行相同的更改。