我正在尝试ODB ORM,并且必须坚持使用接口,因此我需要获取一个const对象并将其持久化。我不确定ODB API是否允许持久保留const对象,因为某些部分似乎已经为此准备好了,但是它不起作用。
我在这里从gcc收到此错误:
void OdbReportRW::write_object(const MyObject &my_object)
{
odb::core::transaction t{db->begin()};
db->persist(my_object);
t.commit();
}
这是错误,我认为它说my_object不应为const:
In file included from /usr/local/include/odb/database.hxx:632:0,
from odb_report.hpp:21,
from src/vcf/odb_report.cpp:18:
/usr/local/include/odb/database.txx: In instantiation of ‘typename odb::object_traits<T>::id_type odb::database::persist_(T&) [with T = const MyObject; odb::database_id DB = (odb::database_id)5u; typename odb::object_traits<T>::id_type = long unsigned int]’:
/usr/local/include/odb/database.ixx:167:45: required from ‘typename odb::object_traits<T>::id_type odb::database::persist(const T&) [with T = MyObject; typename odb::object_traits<T>::id_type = long unsigned int]’
src/vcf/odb_report.cpp:134:26: required from here
/usr/local/include/odb/database.txx:38:39: error: no matching function for call to ‘odb::object_traits_impl<MyObject, (odb::database_id)5u>::persist(odb::database&, const MyObject&)’
object_traits::persist (*this, obj);
^
/usr/local/include/odb/database.txx:38:39: note: candidate is:
In file included from src/vcf/odb_report.cpp:27:0:
my-object-error-odb.hxx:247:5: note: static void odb::access::object_traits_impl<MyObject, (odb::database_id)1u>::persist(odb::database&, odb::access::object_traits<MyObject>::object_type&)
persist (database&, object_type&);
^
my-object-odb.hxx:247:5: note: no known conversion for argument 2 from ‘const MyObject’ to ‘odb::access::object_traits<MyObject>::object_type& {aka MyObject&}’
与c相同的错误,更具描述性:
In file included from src/vcf/odb_report.cpp:18:
In file included from inc/vcf/odb_report.hpp:21:
In file included from /usr/local/include/odb/database.hxx:632:
/usr/local/include/odb/database.txx:38:36: error: binding of reference to type 'object_type' (aka 'MyObject') to a value of type 'const MyObject' drops qualifiers
object_traits::persist (*this, obj);
^~~
/usr/local/include/odb/database.ixx:167:12: note: in instantiation of function template specialization 'odb::database::persist_<const MyObject, 5>' requested here
return persist_<const T, id_common> (obj);
^
src/vcf/odb_report.cpp:134:13: note: in instantiation of function template specialization 'odb::database::persist<MyObject>' requested here
db->persist(my_object);
^
inc/vcf/error-odb.hxx:247:37: note: passing argument to parameter here
persist (database&, object_type&);
^
但是,我可以在数据库界面(从odb中)看到,提供了两种持久性类型:引用和const引用(以及其他带指针的):
// in odb/database.hxx
template <typename T>
typename object_traits<T>::id_type
persist (T& object);
template <typename T>
typename object_traits<T>::id_type
persist (const T& object);
当类中没有要持久化的默认构造函数(此处为MyObject)时find
,我看到了类似的错误(对于,不是persist
),但是它在那里,所以这不是问题。我已经检查过默认构造函数仅find
在生成的代码中生成一个额外的方法。
它可以删除我的write_object
方法中的const说明符,但正如我所说,我必须坚持使用接口。
有什么想法可以保留const对象吗?
更彻底地阅读我发现的文档(http://www.codesynthesis.com/products/odb/doc/manual.xhtml#3.8):
第一个persist()函数期望对要持久存储的实例的常量引用。第二个函数需要一个恒定的对象指针。这两个函数只能在具有应用程序分配的对象ID的对象上使用(第14.4.2节“自动”)。
确实,我在使用auto
指定符来处理数据库处理的ID:
// class MyObject
#pragma db id auto
unsigned long id_;
因此,看来我不能同时使用自动ID和const引用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句