2014-09-19 57 views
-2

我是新來的C++。我的問題是我能夠從我的類中調用Init(),但我無法使用指針從main調用它。經過一些故障排除後,似乎問題在於,當我從main調用Init()時,C++無法看到我傳入的參數(_ip,_user,...)。下面,我有一個可行的例子,第二個例子不行。任何人都可以向我解釋爲什麼第二個例子不起作用?無法使用對象指針訪問類屬性

多謝,

此作品(顯示RES:1):

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <windows.h> 

/* 
Include directly the different 
headers from cppconn/ and mysql_driver.h + mysql_util.h 
(and mysql_connection.h). This will reduce your build time! 
*/ 

#include "mysql_connection.h" 
#include "cppconn/driver.h" 
#include "cppconn/exception.h" 
#include "cppconn/resultset.h" 
#include "cppconn/statement.h" 
#include "cppconn/prepared_statement.h" 

using namespace std; 

class MySQLdb { 

    public: 
     MySQLdb(string ip, string user, string passwd, string schema);  //Create Connection 
     ~MySQLdb(); 

     bool Init();              //Initialize Connection 

    private:   
     const char* _ip;             //MySQL credentials 
     const char* _user; 
     const char* _passwd; 
     const char* _schema; 
     int x = 0; 

     sql::Driver *_driver;            //MySQL attributes 
     sql::Connection *_con; 
     sql::Statement *_stmt; 
     sql::ResultSet *_res; 
     sql::PreparedStatement *_pstmt; 
}; 

MySQLdb::MySQLdb(string ip, string user, string passwd, string schema) 
{ 
    string ip_buffer = "tcp://" + ip; 
    _ip = ip_buffer.c_str(); 
    _user = user.c_str(); 
    _passwd = passwd.c_str(); 
    _schema = schema.c_str(); 

    cout << "res: " << Init(); 
} 
MySQLdb::~MySQLdb() 
{ 
    delete _res; 
    delete _stmt; 
    delete _con; 
} 

bool MySQLdb::Init() 
{ 
    // Create a connection 
    _driver = get_driver_instance(); 
    _con = _driver->connect(_ip, _user, _passwd); 
    _con->setSchema(_schema); 

    if (!_con->isClosed()) 
     return true; 
    else 
     return false; 
} 

int main() 
{ 
    try{ 
     MySQLdb* db = new MySQLdb(IP_ADDRESS, USER, PASSWD, DB); 

     delete db; 
    } 
    catch (sql::SQLException &e) { 

     cout << "# ERR: SQLException in " << __FILE__; 
     cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl; 
     cout << "# ERR: " << e.what(); 
     cout << " (MySQL error code: " << e.getErrorCode(); 
     cout << ", SQLState: " << e.getSQLState() << ")" << endl; 
    } 

    cout << endl; 

    return 0; 
} 

這不起作用(錯誤:無法連接到主機,因爲它無法讀取任何傳入的變量在類的構造函數):

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <windows.h> 

/* 
Include directly the different 
headers from cppconn/ and mysql_driver.h + mysql_util.h 
(and mysql_connection.h). This will reduce your build time! 
*/ 

#include "mysql_connection.h" 
#include "cppconn/driver.h" 
#include "cppconn/exception.h" 
#include "cppconn/resultset.h" 
#include "cppconn/statement.h" 
#include "cppconn/prepared_statement.h" 

using namespace std; 

class MySQLdb { 

    public: 
     MySQLdb(string ip, string user, string passwd, string schema);  //Create Connection 
     ~MySQLdb(); 

     bool Init();              //Initialize Connection 

    private:   
     const char* _ip;             //MySQL credentials 
     const char* _user; 
     const char* _passwd; 
     const char* _schema; 
     int x = 0; 

     sql::Driver *_driver;            //MySQL attributes 
     sql::Connection *_con; 
     sql::Statement *_stmt; 
     sql::ResultSet *_res; 
     sql::PreparedStatement *_pstmt; 
}; 

MySQLdb::MySQLdb(string ip, string user, string passwd, string schema) 
{ 
    string ip_buffer = "tcp://" + ip; 
    _ip = ip_buffer.c_str(); 
    _user = user.c_str(); 
    _passwd = passwd.c_str(); 
    _schema = schema.c_str(); 
} 
MySQLdb::~MySQLdb() 
{ 
    delete _res; 
    delete _stmt; 
    delete _con; 
} 

bool MySQLdb::Init() 
{ 
    // Create a connection 
    _driver = get_driver_instance(); 
    _con = _driver->connect(_ip, _user, _passwd); 
    _con->setSchema(_schema); 

    if (!_con->isClosed()) 
     return true; 
    else 
     return false; 
} 

int main() 
{ 
    try{ 
     MySQLdb* db = new MySQLdb(IP_ADDRESS, USER, PASSWD, DB); 

     db->Init(); 

     delete db; 
    } 
    catch (sql::SQLException &e) { 

     cout << "# ERR: SQLException in " << __FILE__; 
     cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl; 
     cout << "# ERR: " << e.what(); 
     cout << " (MySQL error code: " << e.getErrorCode(); 
     cout << ", SQLState: " << e.getSQLState() << ")" << endl; 
    } 

    cout << endl; 

    return 0; 
} 
+3

您有未定義的行爲。傳遞給構造函數的字符串的生存期結束。一旦字符串被破壞,你從這些字符串獲得的指針值變得無效 – 2014-09-19 00:23:57

+0

謝謝你。這很有道理。 – user2079438 2014-09-19 00:34:53

回答

0

要完成對方的回答,您需要分配並複製內存新的字符串,是這樣的:

string ip_buffer = "tcp://" + ip; 
_ip = new char [ip_buffer.length() + 1]; 
std::strcpy (_ip, ip_buffer.c_str()); 

我不知道,因爲它已經很長一段時間,因爲我在C++編碼,但我認爲你還需要從_ip聲明刪除const attribut。