- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 ODB 建立一对多关系的模型。我基本上是在尝试重新创建 https://www.codesynthesis.com/products/odb/doc/manual.xhtml#6.2.2 中的示例
我必须使用 std::weak_ptr
对于关系的一方,避免循环所有权问题。但是,我的非常简单的示例代码无法编译,因为 ODB 似乎不能很好地与 std::weak_ptr 配合使用。
在我的示例中,每个 Bar
正好有一个Foo
, 而每个 Foo
有多个Bar
秒。这是我的代码:
#include <odb/core.hxx>
#include <string>
#include <memory>
#include <vector>
// Forward
class Foo;
#pragma db object
class Bar {
public:
// A Bar has exactly *one* Foo
#pragma db not_null
std::shared_ptr<Foo> cfg;
private:
#pragma db id auto
unsigned long id_;
friend class odb::access;
};
#pragma db object
class Foo {
public:
// A Foo has multiple Bars
// Using std::weak_ptr here instead of std::shared_ptr to avoid circular
// ownership
#pragma db value_not_null inverse(cfg)
std::vector<std::weak_ptr<Bar>> entries;
private:
#pragma db id auto
unsigned long id_;
friend class odb::access;
};
int main() {}
我生成数据库代码:
odb --std c++11 --database sqlite --generate-query --generate-schema --at-once main.hpp
然后我这样编译:
g++ --std=c++11 main.hpp main-odb.cxx
(我知道这会在链接时崩溃 - 我只是想让它编译。)
我的编译器 (GCC 7) 告诉我:
main-odb.cxx: In static member function ‘static void odb::access::object_traits_impl<Foo, (odb::database_id)1u>::entries_traits::init(odb::access::object_traits_impl<Foo, (odb::database_id)1u>::entries_traits::value_type&, const odb::access::object_traits_impl<Foo, (odb::database_id)1u>::entries_traits::data_image_type&, odb::database*)’:
main-odb.cxx:794:43: error: no matching function for call to ‘std::weak_ptr<Bar>::weak_ptr(odb::object_traits<Bar>::pointer_type)’
obj_traits::object_type > (id));
^
In file included from /usr/include/c++/5/memory:82:0,
from main.hpp:3,
from main-odb.hxx:16,
from main-odb.cxx:7:
/usr/include/c++/5/bits/shared_ptr.h:492:2: note: candidate: template<class _Tp1, class> std::weak_ptr<_Tp>::weak_ptr(std::weak_ptr<_Tp1>&&)
weak_ptr(weak_ptr<_Tp1>&& __r) noexcept
^
/usr/include/c++/5/bits/shared_ptr.h:492:2: note: template argument deduction/substitution failed:
main-odb.cxx:794:43: note: mismatched types ‘std::weak_ptr<_Tp>’ and ‘odb::object_traits<Bar>::pointer_type {aka Bar*}’
obj_traits::object_type > (id));
我跳过了另外三个候选人。重要的部分:ODB 试图在某处创建一个 std::weak_ptr<Bar>
来自Bar *
,这显然是不可能的。它必须从 std::shared_ptr<Bar>
创建它.但是,ODB 文档明确表示应该(事实上必须)使用 std::weak_ptr
在这些情况下。
我做错了什么?
最佳答案
好的,我想出了一个解决方案(不确定这是不是最好的解决方案):
您可以强制 ODB 使用 std::shared_ptr<Bar>
而不是 Bar *
到处。您可以通过将类定义为:
#pragma db object pointer(std::shared_ptr)
class Bar {
...
这样,当一个 std::weak_ptr<Bar>
正在创建,它是从 std::shared_ptr<Bar>
创建的,这有效。您还可以指定要在 namespace 或全局范围内使用的指针类型,请参阅 https://www.codesynthesis.com/products/odb/doc/manual.xhtml#3.3
关于C++ ODB 数据库映射器:无法在关系中使用 std::weak_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51652818/
我想了解 ODB 模型是如何工作的。 这是来自 the official site 的片段: #pragma db object class person { private:
我第一次使用 ODB:Code Synthesis 的 C++ 对象关系映射 (ORM)。这是一个类似于 C# 的 .NET EntityFramework 的工具,因为它允许您通过 C++ 在各种关
我一直在看ODB ORM现在已经有一段时间了,并进行了一些练习。我的问题是在重新编译代码的不同 DBMS 之间切换。从我的 Java 背景来看,我可以简单地更改配置文件并且 ORM 可以工作,例如 H
在我的 Visual Studio 解决方案中,我有两个不同的项目,其中一个构建为 static library,另一个构建为可执行文件。 ODB 相关代码,如创建数据库等都在库中。我将这个库链接到我
如何在 Mac OS X 中将 MacVim 配置为 ODB 编辑器?我有狮子(10.7),但我想雪豹也是一样。 MacVim 文档和简单的 Google 搜索似乎都缺少此过程,尽管 MacVim 的
我必须使用 ODB framework在我的项目中。我有 2 个文件:station.hxx 和 common.hxx,内容如下: common.hxx #ifndef COMMON_HXX #def
Microsoft SQL Server 上存储了一个过程。过程可以返回一个值。如何使用 ODB ORM 从 MS SQL Server 数据库执行存储过程并获取该过程返回的值? 最佳答案 您可以为此
是否可以将 odb(c++ orm 类框架)编译指示移到类头之外?例如,我只用 id 定义类 basic_object(抽象): class basic_object { int _id; p
我对这个库和一般的 ORM 还很陌生。我知道像 EntityFramework 这样的工具可以从数据库生成代码。 ODB for C++ 可以完成这项工作吗? 最佳答案 我不这么认为。看着 main
我最近完成了本教程, http://www.objectdb.com/tutorial/jpa/eclipse/spring/run 它说在Tomcat目录下创建了.odb文件,但是我找不到Tomca
使用 VS 2012 和 Microsoft SQL Server。我得到了所有要编译和构建的东西,但是当我尝试保留第一个对象时程序就死了: // file : hello/driver.c
我使用以下查询通过 JDBC 从 .mdb 文件检索数据,但是当我在 .odb 文件上尝试它时,它不会抛出任何异常,但根本没有结果。我想知道 .odb 是否区分大小写,而 .mdb 不区分大小写,还是
我关注了libgit2-backend为 libgit2 ODB 后端创建 mysql 后端的项目。 创建 blob 对象并将其放入 mysql 支持的 ODB 中相当容易,我正在使用 git_blo
我有一个名为 TimelineItem 的对象,它有一个名为 linked_items_ 的数据字段,其中包含一个 TimelineItem 类型的 vector 。这是为了表示链接到原始项目(重复项
我的代码有什么问题吗?继续显示此错误消息 Microsoft OLE DB Provider for ODBC Drivers error '80040e14' [MySQL][ODBC 5.2(w)
我正在尝试使用 ODB 建立一对多关系的模型。我基本上是在尝试重新创建 https://www.codesynthesis.com/products/odb/doc/manual.xhtml#6.2.
我正在考虑使用 ORM(对象关系映射器)来将我的 C++ 对象保存到 SQLite 数据库中。我目前正在考虑通过 CodeSynthesis 进行 ODB。 参见:http://www.codesyn
我正在尝试按照本教程学习如何使用 C++ 和 ODB: http://www.codesynthesis.com/products/odb/doc/manual.xhtml#2 我创建了一个 Pers
我想在我的游戏中使用 artemis ( https://github.com/junkdog/artemis-odb)。 最近我读到了 Glenn Fiedler 的游戏循环: http://gaf
我正在尝试使用 ODB Java API (this library)从我的汽车通过蓝牙从 ELM327 获取数据,但在每次请求时返回 ?,并且库引发 MisunderstoodException 这
我是一名优秀的程序员,十分优秀!