Okay, I just directly put my code here:

main.cpp

mysqlpp::Connection conn(false);

int main() {
if (conn.connect(DATABASE, HOST, DBUSER, DBPASS)) {
    Users a(conn, "test","pass");
    //a.authenticate();

and my Users.cpp:

Users::Users(mysqlpp::Connection conn, string username, string password) {
    this->conn = conn;
    this->username = username;
    this->password = password;
}

I can compile the program with no errors, but in command line, it says "Segmentation fault". Whats this error? and how to solve it?

有帮助吗?

解决方案

you should store a pointer to a mysqlpp::Connection in your Users class then accept a pointer instead of a whole connection object in your constructor. Don't forget to update other references in your class implementation as well.

main.cpp:

replace

mysqlpp::Connection conn(false);

with

mysqlpp::Connection *conn = new mysqlpp::Connection(false);

Users.cpp

replace

Users::Users(mysqlpp::Connection conn, string username, string password) {
this->conn = conn;

with

Users::Users(mysqlpp::Connection *conn, string username, string password) {
this->conn = conn;

as long as in your Users.h it says something like this

class Users
{
    private:
        mysqlpp::Connection *conn; // <-- the star is important!
    // rest of class definition goes here
};

then you access the methods of conn like conn->method()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top