ODB 无效使用不完整类型‘classodb::access::object_traits_impl<Person, (odb::database_id)1>’

ODB invalid use of incomplete type ‘class odb::access::object_traits_impl<Person, (odb::database_id)1>’

我写了太简单的 odb 示例并从文档中复制了所有代码但是在调用 db.persist(john) 编译器时说

/usr/include/odb/traits.hxx: In instantiation of ‘struct odb::object_traits_impl’: /usr/include/odb/database.txx:61:28: required from ‘typename odb::object_traits::id_type odb::database::persist_(const typename odb::object_traits::pointer_type&) [with T = Person; odb::database_id DB = (odb::database_id)1; typename odb::object_traits::id_type = odb::access::object_traits::id_type; typename odb::object_traits::pointer_type = Person*]’ /usr/include/odb/sqlite/database.ixx:47:37: required from ‘typename odb::object_traits::id_type odb::sqlite::database::persist(T*) [with T = Person; typename odb::object_traits::id_type = odb::access::object_traits::id_type]’ /home/mohsen/Desktop/Workstation/Projects/C++/Rubik/main.cpp:25:21: required from here /usr/include/odb/traits.hxx:177:10: error: invalid use of incomplete type ‘class odb::access::object_traits_impl’ struct object_traits_impl:

怎么了?

我的代码:

main.cpp

using namespace std;
using namespace odb::core;

int main(int argc, char **argv) {

odb::sqlite::database db("sqlite.db", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);

    Person john("John", "Doe", 33);
//    Person jane("Jane", "Doe", 32);
//    Person joe("Joe", "Dirt", 30);

//    odb::transaction t(db.begin());
    db.persist(john);
//    t.commit();

    return 0;
}

Person.h

#pragma db object
class Person
{
public:
    Person (const std::string& first,
            const std::string& last,
            unsigned short age)
            : first_ (first), last_ (last), age_ (age)
    {
    }

    const std::string&
    first () const
    {
        return first_;
    }

    const std::string&
    last () const
    {
        return last_;
    }

    unsigned short
    age () const
    {
        return age_;
    }

    void
    age (unsigned short age)
    {
        age_ = age;
    }

private:
    friend class odb::access;

    Person () {}

#pragma db id auto
    unsigned long id_;

    std::string first_;
    std::string last_;
    unsigned short age_;
};

odb编译命令

odb -dsqlite --generate-query --generate-session --generate-schema --generate-prepared --output -dir/home/mohsen/Desktop/Workstation/Projects/C++/Rubik/cmake-build-debug/odb_gen --hxx-suffix.h --ixx-suffix_inline.h --cxx-suffix.cpp --odb-file-suffix_odbPerson.h

您收到此错误是因为 ‘class odb::access::object_traits_impl<Person, (odb::database_id)1>’ 是在 odb 生成的文件 Person-odb.hxx 中定义的。

在 main.cpp 文件的开头包含 "Person-odb.hxx" 可以解决您的问题。