[odb-users] Loading of lazy ptr within odb object class
Boris Kolpackov
boris at codesynthesis.com
Mon Feb 25 06:15:11 EST 2019
Lisa Fiedler <lfiedler at informatik.uni-leipzig.de> writes:
> #pragma db object pointer(std::shared_ptr) session table("schema1.employee")
> class employee
> {
>
> [...]
>
> void printEmployer(){
> cout << emPloyer_.load()->name() << endl;
> }
>
> [...]
>
> "error: no match for ‘operator=’ (operand types are
> ‘std::shared_ptr<employer>’ and ‘odb::object_traits<employer>::pointer_type {aka employer*}’)
> p_ = i_.template load<T> (true); // Reset id"
>
> If I execute this operation, i.e. emPloyer.load()->name(), for instance in
> the main method it executes just fine. I suspected the reason is, that in
> main.cpp I included employee-odb.hxx, which I did not do in employee.hxx
> where the above mentioned classes are defined. I therefore tried to include
> it there, but this apparently leads to some cyclic inclusions.
Yes, to be able to load a persistent object you need to have its
database support code included (the corresponding *-odb.hxx file).
The easiest way to resolve this problem is to move the definition
of printEmployer() to the source file. For example:
// file: employee.hxx
...
#pragma db object pointer(std::shared_ptr) session table("schema1.employee")
class employee
{
...
void printEmployer();
...
};
// file: employee.cxx
...
#include "employer-odb.hxx"
void employee::printEmployer() {
cout << emPloyer_.load()->name() << endl;
}
More information about the odb-users
mailing list