2011-04-23 93 views
1

剛接觸C++,需要幫助解決一個錯誤。 下面的代碼在較舊的Sun版本(CC 5.3和5.8)上編譯得很好。 我在發生錯誤的行上發表評論。 任何援助將不勝感激。 非常感謝。 -RW初始化一個對象時發生C++編譯錯誤

Unix版:SunOS的ut51278 5.10 Generic_141444-09 sun4u的SPARC SUNW,SPARC-企業
編譯器版本:CC:孫C++ 5.11 SunOS_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存在的......看到錯誤messag評論È....

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; 
} 

回答

1

變化:

DBSql(DBSql& dbSql); 

到:

DBSql(DBSql const & dbSql); 

調用Table(string(tableName))會產生一個臨時文件,需要使用複製構造函數進行復制才能返回。由於該值是暫時的,因此您只能獲得對其的引用 - 而您的副本構造函數不接受該值。

因爲你的拷貝構造函數和賦值操作符實際上並不是做的什麼,所以你可以把它們排除在外。如果保留它們,則還應該更改DBSql& operator=(DBSql& dbSql);以接受const引用 - DBSql& operator=(DBSql const & dbSql); - 並在父類複製構造函數和賦值運算符中進行相同的更改。