Project

General

Profile

Actions

Support #4114

closed

compilation error with Wt::Dbo::dbo_traits<>: previous definition

Added by Marie Freslier almost 9 years ago. Updated over 8 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
Start date:
05/26/2015
Due date:
% Done:

0%

Estimated time:

Description

Hello,

I've created a database with (at the moment) 3 tables: Lab, Device and User. I took the User Table from the Auth example. I defined for the Lab and the Device new Id with the dbo_traits. I had no problem for the Device class but it dosn't work for the Lab class. I could determine that the problem comes from the relation between the Lab and the User class. The error by compilation is :

Lab.h:43:12: error: specialization of ‘Wt::Dbo::dbo_traits<Lab>’ after instantiation
Lab.h:43:12: error: redefinition of ‘struct Wt::Dbo::dbo_traits<Lab>’
In file included from /usr/include/Wt/Dbo/Session:19:0,
/usr/include/Wt/Dbo/ptr:206:8: error: previous definition of ‘struct Wt::Dbo::dbo_traits<Lab>’

Could you help me?

I copy hier a simplified version of the classes:

User.h

#ifndef USER_H_
#define USER_H_

#include <string>
#include <Wt/Dbo/Types>
#include <Wt/WGlobal>

#include "Lab.h"

using namespace std;
namespace dbo = Wt::Dbo;

class Lab;

class User;
typedef Wt::Auth::Dbo::AuthInfo<User> AuthInfo;

class User {
    public:
    /** variables **/
    ...

    /******************
     *** relations ***/
    dbo::collection< dbo::ptr<Lab> > user_labInCharge;

  template<class Action>
  void persist(Action& a)
  {
      /** variables **/
      ...

      /******************
       *** relations ***/
      dbo::hasMany(a, user_labInCharge, dbo::ManyToOne, "labChief");
  }
};

DBO_EXTERN_TEMPLATES(User);

#endif // USER_H_

User.cc

#include "User.h"

#include <Wt/Dbo/Impl>
#include <Wt/Auth/Dbo/AuthInfo>

DBO_INSTANTIATE_TEMPLATES(User);

Lab.h

#ifndef LAB_H_
#define LAB_H_

#include <string>
#include <Wt/Dbo/Dbo>

#include "Device.h"
#include "User.h"

using namespace Wt;
using namespace std;
namespace dbo = Wt::Dbo;

/** define the labSurname as primary key of class Lab **/
class Lab;

namespace Wt {
namespace Dbo {
    template<>
    struct dbo_traits<Lab> : public dbo_default_traits
    {
       typedef string IdType;
       static IdType invalidId() { return string(); }
       static const char *surrogateIdField() { return 0; }
    };
  }
}

class Device;
class User;

class Lab
{
    public:
    /** primary key **/
    string labSurname;

    /** variables **/
        ...

    /******************
     *** relations ***/
    dbo::ptr<User> labInChargeUser;
    dbo::collection< dbo::ptr<Device> > lab_devices;

    template<class Action>
    void persist(Action& a)
    {
        /** primary key **/
        dbo::id(a,labSurname,"lab_id",20);

        /** variables **/
        ...

        /******************
         *** relations ***/
        dbo::belongsTo(a, labInChargeUser, "labChief");
        dbo::hasMany(a, lab_devices, dbo::ManyToOne, "labDevices");
    }
};
#endif //LAB_H_

Device.h

#ifndef DEVICE_H_
#define DEVICE_H_

#include <string>
#include <Wt/Dbo/Dbo>

#include "Lab.h"

using namespace Wt;
using namespace std;
namespace dbo = Wt::Dbo;

/** define the devName as primary key of class Device**/
class Device;

namespace Wt {
  namespace Dbo {

    template<>
    struct dbo_traits<Device> : public dbo_default_traits
    {
      typedef string IdType;
      static IdType invalidId() { return string(); }
      static const char *surrogateIdField() { return 0; }
    };
  }
}

class Lab;

class Device
{
    public:
    /** primary key **/
    string devName;

    /** variables **/
    ...

    /******************
     *** relations ***/
    dbo::ptr<Lab> device_Lab;

    template<class Action>
    void persist(Action& a)
    {
        /** primary key **/
        dbo::id(a,devName,"dev_id",20);

        /** variables **/
        ...

        /******************
         *** relations ***/
        dbo::belongsTo(a, device_Lab, "labDevices");
    }
};
#endif //DEVICE_H_
Actions #1

Updated by Koen Deforche almost 9 years ago

  • Status changed from New to Resolved
  • Assignee set to Koen Deforche

Hey,

You need to make sure you declare the specialization as soon as you can:

#ifndef LAB_H_
#define LAB_H_

#include <string>
#include <Wt/Dbo/Dbo>

using namespace Wt;
using namespace std;
namespace dbo = Wt::Dbo;

/** define the labSurname as primary key of class Lab **/
class Lab;

namespace Wt {
namespace Dbo {
    template<>
    struct dbo_traits<Lab> : public dbo_default_traits
    {
       typedef string IdType;
       static IdType invalidId() { return string(); }
       static const char *surrogateIdField() { return 0; }
    };
  }
}

#include "User.h" 
#include "Device.h" 

...
Actions #2

Updated by Marie Freslier almost 9 years ago

Great!

Thanks a lot!

Best regards

Marie

Actions #3

Updated by Koen Deforche over 8 years ago

  • Status changed from Resolved to Closed
Actions #4

Updated by Koen Deforche over 8 years ago

  • Target version set to 3.3.5
Actions

Also available in: Atom PDF