mardi 21 mai 2019

Declaring sqlite::database db(":memory:") in header gives error

I am developing a sample program using sqlite_modern_cpp provided on their github page. I am using in-memory database.

In my final application I need to store this database as a private member of my class. I am getting an error when declaring the database object sqlite::database db(":memory:"); as private member. The error gets resolved when I move the declaration sqlite::database db(":memory:"); to the source file.

#include <memory>
#include <sqlite_modern_cpp.h>
class sqlitemoderncpp
{
private :
    sqlite::database db(":memory:");   //Declaring in header gives error but all errors are resolved when moded to source file
public:
    void run();
};

void sqlitecpp::run(void)
{


try {
        db <<
            "create table if not exists user ("
            "   _id integer primary key autoincrement not null,"
            "   age int,"
            "   name text,"
            "   weight real"
            ");";
        int age = 21;
        float weight = 68.5;
        std::string name = "jack";
        this->db << "insert into user (age,name,weight) values (?,?,?);" 
            << age
            << name
            << weight;

        std::cout << "The new record got assigned id " << this->db.last_insert_rowid() << std::endl;

        db << "select age,name,weight from user where age > ? ;"
            << 18
            >> [&](int age, std::string name, double weight) {
            std::cout << age << ' ' << name << ' ' << weight << std::endl;
        };
    }
    catch (std::exception& e) {
        std::cout << e.what() << std::endl;
    }
}

Error :

error C3867: 'sqlitecpp::db': non-standard syntax; use '&' to create a pointer to member 

error C2296: '<<': illegal, left operand has type 'sqlite::database (__cdecl sqlitecpp::* )(void)' 

error C2297: '<<': illegal, right operand has type 'const char [124]' 

error C3867: 'sqlitecpp::db': non-standard syntax; use '&' to create a pointer to member 

error C2296: '<<': illegal, left operand has type 'sqlite::database (__cdecl sqlitecpp::* )(void)' 

error C2297: '<<': illegal, right operand has type 'const char [51]' error 

C3867: 'sqlitecpp::db': non-standard syntax; use '&' to create a pointer to member error C2296: '<<': illegal, left operand has type 'sqlite::database (__cdecl sqlitecpp::* )(void)' 

error C2297: '<<': illegal, right operand has type 'const char [49]'

Aucun commentaire:

Enregistrer un commentaire