Project

General

Profile

WT::DBO - Specialization of ‘Wt::Dbo::dbo_traits<User>’ after instantiation

Added by Charles Vegara over 3 years ago

Hello.

I'm getting this error when trying to remove a surrogate key from a mapping.

Specialization of 'Wt::Dbo::dbo_traits' after instantiation

The code I've added:

namespace Wt {
    namespace Dbo {
        template<>
        struct dbo_traits<User> : public dbo_default_traits {

            static const char *surrogateIdField() { return nullptr; }
        };
    }
}

If I remove this block of code, code is compiled.

I have attached a simple project that causes the compile problem I'm having.

Thanks in advance and best regards.

The code I'm testing:

post.h

#ifndef POST_H
#define POST_H

#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/ptr.h>

class User;
class Post;
namespace Wt {
    namespace Dbo {
        template<>
        struct dbo_traits<Post> : public dbo_default_traits {

            static const char *surrogateIdField() { return nullptr; }
        };
    }
}

class Post
{
public:
    Post();
    long myid;
    Wt::Dbo::ptr<User> user;

    template<class Action>
    void persist(Action& a)
    {
        Wt::Dbo::id(a, myid, "myid");
        Wt::Dbo::belongsTo(a, user, "user");
    }
};

#endif // POST_H

user.h

#ifndef USER_H
#define USER_H

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

class Post;
class User;
namespace Wt {
    namespace Dbo {
        template<>
        struct dbo_traits<User> : public dbo_default_traits {

            static const char *surrogateIdField() { return nullptr; }
        };
    }
}

class User
{
public:
    User();
    long myid;
    std::string name;
    Wt::Dbo::collection< Wt::Dbo::ptr<Post> > posts;

    template<class Action>
    void persist(Action& a)
    {
        Wt::Dbo::id(a, myid, "myid");
        Wt::Dbo::field(a, name, "name");
        Wt::Dbo::hasMany(a, posts, Wt::Dbo::ManyToOne, "user");
    }
};

#endif // USER_H

sess.h

#ifndef SESS_H
#define SESS_H

#include <Wt/Dbo/ptr.h>

class User;
class Post;

class Sess
{
public:
    Sess();
    static Wt::Dbo::ptr<User> userById(long const id);

private:
    Wt::Dbo::ptr<User> mUser;
    Wt::Dbo::ptr<Post> mPost;
};

#endif // SESS_H

sess.cpp

#include "sess.h"

#include "user.h"
#include "post.h"

Sess::Sess()
{
}

Wt::Dbo::ptr<User> Sess::userById(const long /*id*/)
{
    return Wt::Dbo::ptr<User>(nullptr);
}

main.cpp

#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/ptr.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include <string>

#include "user.h"
#include "post.h"
#include "sess.h"


int main(int argc, char *argv[])
{
    std::unique_ptr<Wt::Dbo::backend::Sqlite3> sqlite3(new Wt::Dbo::backend::Sqlite3(":memory:"));
    sqlite3->setProperty("show-queries", "true");
    Wt::Dbo::Session session;
    session.setConnection(std::move(sqlite3));

    session.mapClass<User>("user");
    session.mapClass<Post>("post");

    Wt::Dbo::ptr<User> a =  Sess::userById(1);

    a.modify()->name = "";

    return 0;
}
test.7z (3.49 KB) test.7z project with Specialization after instantiation problem

Replies (1)

RE: WT::DBO - Specialization of ‘Wt::Dbo::dbo_traits<User>’ after instantiation - Added by Charles Vegara over 3 years ago

Solve it by removing the forward declaration sess.h.

Sorry and thank you.

    (1-1/1)