在标头中声明sqlite :: database db(“:memory:”)会出错

时间:2019-05-21 10:21:27

标签: c++ c++11

我正在使用其github页面上提供的sqlite_modern_cpp开发示例程序。我正在使用内存数据库。

在我的最终应用程序中,我需要将此数据库存储为班级的私有成员。将数据库对象sqlite::database db(":memory:");声明为私有时出现错误 会员。将声明sqlite::database db(":memory:");移动到源文件时,该错误得到解决。

#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 sqlitemoderncpp::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 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]'

1 个答案:

答案 0 :(得分:1)

default member initializer(自C ++ 11起)仅适用于花括号或等于初始值设定项。您可以将其更改为

${Variable_name} = Get Text <location>

Set Suite Variable  ${Variable_name_to_use_later}  ${Variable_name}

sqlite::database db{":memory:"};

在C ++ 11之前,您可以添加带有member initializer list的构造函数。

sqlite::database db = sqlite::database(":memory:");