无法使用对象指针访问类属性

时间:2014-09-19 00:21:07

标签: c++ mysql

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

1 个答案:

答案 0 :(得分:0)

要完成另一个答案,您需要为新字符串分配和复制内存,例如:

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

我不确定,因为我用C ++编码已经很长时间了,但我认为您还需要从const的声明中删除_ip attribut。