[odb-users] help

khaldon hmesheh khaldon_hmesheh at yahoo.com
Mon May 14 04:28:37 EDT 2012


hi,

I have written the following code and I want to try how the relations work. Here is the class and relations definitions

ship--->generalCharacteristic;

ship<---->assembly

assembly--->dreiDPoint


#ifndef SHIP_HXX
#define SHIP_HXX

#include <string>
#include <vector>
#include <cstddef> // std::size_t

#include <odb/core.hxx>
#include <odb/tr1/memory.hxx>

#include <odb/tr1/lazy-ptr.hxx>

using std::tr1::shared_ptr;

using odb::tr1::lazy_shared_ptr;
using odb::tr1::lazy_weak_ptr;

// The following bidirectional relationships are used:
//
// one-to-many  : ship <--> Assembly
// to-one   : ship --> generalCharacteristics
//to-one   : assembly --> 3DPoint


// Forward declarations.
//
class ship;
class generalCharacteristic;
class assembly;
class dreiDPoint;

typedef std::vector<lazy_weak_ptr<assembly> > assemblies;

enum type {barge, bargeForDeckLoading, bargeForLiquefiedGas, bargForOil};

#pragma db object
class generalCharacteristic
{
public:
  generalCharacteristic (const float& lpp 	 )
      : lpp_ (lpp)
{
}
/////length between the perpendicular
float lpp () const
{
return lpp_;
}

void lpp (float lpp)
{
lpp_ = lpp;
 }

private:
friend class odb::access;

generalCharacteristic () {}

#pragma db id auto
unsigned long id_;

float lpp_;
  };

#pragma db object
class assembly
{
public:
typedef ::ship ship_type;
typedef ::dreiDPoint dreiDPoint_type;

assembly (float weight,
 		lazy_shared_ptr<dreiDPoint_type> cog)
        : weight_ (weight), cog_ (cog)
{
}
float weight() const
{
return weight_;
}
void weight (float weight)
{
weight_=weight;
}

// Ship.
//
const lazy_weak_ptr<ship_type>&
ship () const
{
return ship_;
}

void
ship (lazy_weak_ptr<ship_type> ship)
{
ship_ = ship;
}

// 3D Point of this COG.
//
const lazy_shared_ptr<dreiDPoint_type>&
cog () const
{
return cog_;
}

void
cog (lazy_shared_ptr<dreiDPoint_type> cog)
{
cog_ = cog;
   }
 
private:
friend class odb::access;

assembly () {}

float weight_;

#pragma db id auto
unsigned long assemId_;

#pragma db value_not_null inverse (assemblies_)
lazy_weak_ptr<ship_type> ship_;

#pragma db null
lazy_shared_ptr<dreiDPoint_type> cog_;

};

#pragma db object
class dreiDPoint
{
public:
  dreiDPoint (float xValue)
      : xValue_ (xValue)
{
}

/////x coordinate of the point
float xValue () const
{
return xValue_;
}

void xValue (float xValue)
{
xValue_ = xValue;
}
 
private:
friend class odb::access;

dreiDPoint () {}

#pragma db id auto
unsigned long id_;

float xValue_;
  };

#pragma db object
class ship
{
public:
typedef ::generalCharacteristic generalCharacteristic_type;
typedef ::assemblies assembly_type;


ship (unsigned int imoId,
unsigned short buildingYear,
type shipType,
const std::string& name,
lazy_shared_ptr<generalCharacteristic_type> generalCharacteristic)

: imoId_ (imoId), buildingYear_ (buildingYear), shipType_ (shipType),name_(name), generalCharacteristic_ (generalCharacteristic)
{
}

unsigned int  imoId () const
{
return imoId_;
}
void imoId (unsigned int imoId) 
{
imoId_=imoId;
}

unsigned short   buildingYear () const
{
return buildingYear_;
}
void buildingYear (unsigned short buildingYear) 
{
buildingYear_=buildingYear;
}

const std::string&  name () const
{
return name_;
}
void  name (std::string name) 
{
name_=name;
}

type shipType () const
{
return shipType_;
}
void shipType (type shipType) 
{
shipType_=shipType;
}

// Assemblies of this ship
const assembly_type&
assemblies () const
{
return assemblies_;
}

assembly_type&
assemblies () 
{
return assemblies_;
}

// General Charachteristics of this ship.
//
lazy_shared_ptr<generalCharacteristic_type> generalCharacteristic () const
{
return generalCharacteristic_;
}

void generalCharacteristic (lazy_shared_ptr<generalCharacteristic_type> generalCharacteristic)
{
generalCharacteristic_ = generalCharacteristic;
}

private:
friend class odb::access;
ship () {}

#pragma db id auto
unsigned long id_;

unsigned int imoId_;
unsigned short buildingYear_;
std::string name_;
type shipType_;

#pragma db value_null unordered
assembly_type assemblies_;

#pragma db not_null
lazy_shared_ptr<generalCharacteristic_type> generalCharacteristic_;

}; 

#endif // SHIP_HXX
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

and I have written something like the following code in driver:

#include <memory>   // std::auto_ptr
#include <iostream>

#include <odb/database.hxx>
#include <odb/session.hxx>
#include <odb/transaction.hxx>

#include "database.hxx" // create_database

#include "ship.hxx"
#include "ship-odb.hxx"

using namespace std;
using namespace odb::core;


int
main (int argc, char* argv[])
{
try
{
auto_ptr<database> db (create_database (argc, argv));
unsigned long karam1_id, karam2_id;
// Create a few persistent objects.
//

{
        shared_ptr<generalCharacteristic> gc (new generalCharacteristic (100.2));

        shared_ptr<dreiDPoint> dp (new dreiDPoint (5.2));


        shared_ptr<assembly> assem (new assembly (1200,dp));

shared_ptr<ship> karam (new ship (1500,1995,barge,"Karam1",gc));

// Set the inverse side of the ship-assemblies relationship.
//
karam->assemblies().push_back(assem);

// Set the ship-generalCharacteristic relationship.
//
karam->generalCharacteristic() = gc;

transaction t (db->begin ());
db->persist (gc);

db->persist (dp);
db->persist (assem);

karam1_id = db->persist (karam);
t.commit ();
}
 
{
session s;
transaction t (db->begin());
shared_ptr<ship> p (db->load<ship> (karam1_id)); 
 	cout<<" general charac: "<<p->generalCharacteristic_->lpp_<<endl; ////////Here I got the problem
t.commit();
}
 
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}


I got the following error messages:
error C2248: 'ship::generalCharacteristic_' : cannot access private member declared in class 'ship'

error C2248: 'generalCharacteristic::lpp_' : cannot access private member declared in class 'generalCharacteristic'

Although I have declared the relationship and the access rights between ship and generalCharacteristic classes
Any help I appreciate.
Khaldon


More information about the odb-users mailing list