2016-07-04 44 views
1

我正在嘗試ODB ORM,我必須堅持一個接口,所以我需要一個const對象並堅持下去。我不確定ODB API是否允許持久化一個const對象,因爲有些部分似乎已經準備好了,但它不起作用。我可以將一個const對象傳遞給ODB中的db.persist嗎?

我從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&}’ 

鏗鏘同樣的錯誤,一位更多的描述:

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); 

我看到一個類似的錯誤引發(用於find,不persist)時,有在課堂上堅持沒有默認構造函數(爲MyObject這裏),但它在那裏,所以它不是問題。我已經檢查過,默認構造函數僅在生成的代碼中生成一個額外的方法find

它的工作除去我的write_object方法中的const說明符,但正如我所說的,我必須堅持一個接口。

任何想法堅持const對象?

回答

1

讀取更徹底我發現這個文檔(http://www.codesynthesis.com/products/odb/doc/manual.xhtml#3.8):

第一持續()函數需要一個恆定參考實例被持續。第二個函數需要一個常量對象指針。這兩個函數只能用於具有應用程序分配對象ID的對象(第14.4.2節「自動」)。

實在的,我用的是auto說明符數據庫處理IDS:

// class MyObject  
#pragma db id auto 
unsigned long id_; 

這樣看來,我不能在同一時間自動ID和常量引用堅持使用。

相關問題