首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >双向链路odb失败

双向链路odb失败
EN

Stack Overflow用户
提问于 2022-09-05 23:23:03
回答 1查看 32关注 0票数 0

我有两个具有双向链接的类,但是使用以下命令生成odb文件失败了:

代码语言:javascript
复制
odb --database mysql --generate-query --generate-schema --std c++14  -I./data-bdd/src/model *.hxx

我有这个错误(使用MySQL和SQLite数据库类型)

代码语言:javascript
复制
error: unable to map C++ type '::__gnu_cxx::new_allocator< ::std::weak_ptr< ::message > >::value_type' used in data member 'm_messages' to a MySQL database type
chat.hxx:29:39: info: use '#pragma db value_type' to specify the database type

我试着添加没有效果的语用

代码语言:javascript
复制
 #pragma db object pointer(std::shared_ptr)

源文件:

在chat.hxx中

代码语言:javascript
复制
#include "./root_model_object.hxx"

 #include <memory>
 #include <string>
 #include <vector>

 class message;
 class user;

 /**
  * @brief class of chat object in model
  *
  */
 #pragma db object pointer(std::shared_ptr)
 class chat final : public root_model_object {
 private:
   friend class odb::access;
   std::string m_name;
 #pragma db value_not_null inverse(m_chat)
   std::vector<std::weak_ptr<message>> m_messages;
 #pragma db value_not_null inverse(m_chats)
   std::vector<std::weak_ptr<user>> m_members;

 public:
   /**
    * @brief Construct a new chat object
    *
    */
   chat() = default;

   /**
    * @brief Set the name of object
    *
    * @param name The name of object
    */
   void setName(const std::string &name) { m_name = name; };

   /**
    * @brief Get the name of object
    *
    * @return const std::string& the name of object
    */
   const std::string &getName() const { return m_name; };

   /**
    * @brief Set the messages of object
    *
    * @param messages The messages of object
    */
   void setMessages(const std::vector<std::weak_ptr<message>> &messages) {
     m_messages = messages;
   };

   /**
    * @brief Get the messages of object
    *
    * @return const std::vector<std::weak_ptr<message>>& the messages of object
    */
   const std::vector<std::weak_ptr<message>> &getMessages() const {
     return m_messages;
   };

   /**
    * @brief Set the members of object
    *
    * @param members The members of object
    */
   void setMembers(const std::vector<std::weak_ptr<user>> &members) {
     m_members = members;
   };

   /**
    * @brief Get the members of object
    *
    * @return const std::vector<std::weak_ptr<user>>& the members of object
    */
   const std::vector<std::weak_ptr<user>> &getMembers() const {
     return m_members;
   };
 };

 #pragma db object(chat)
 };

在message.hxx中

代码语言:javascript
复制
#include "./chat.hxx"
 #include "./root_model_object.hxx"
 #include "./user.hxx"

 #include <ctime>
 #include <memory>
 #include <string>

 /**
  * @brief class of message object in model
  *
  */
 #pragma db object pointer(std::shared_ptr)
 class message final : public root_model_object {
 private:
   friend class odb::access;
   std::string m_content;
 #pragma db not_null
   std::shared_ptr<user> m_sender;
   std::time_t m_send_date;
 #pragma db not_null
   std::shared_ptr<chat> m_chat;

 public:
   /**
    * @brief Construct a new message object
    *
    */
   message() = default;

   /**
    * @brief Set the content of object
    *
    * @param content The content of object
    */
   void setContent(const std::string &content) { m_content = content; };

   /**
    * @brief Get the content of object
    *
    * @return const std::string& the content of object
    */
   const std::string &getContent() const { return m_content; };

   /**
    * @brief Set the sender of object
    *
    * @param sender The sender of object
    */
   void setSender(const std::shared_ptr<user> &sender) { m_sender = sender; };

   /**
    * @brief Get the sender of object
    *
    * @return const std::shared_ptr<user>& the sender of object
    */
   const std::shared_ptr<user> &getSender() const { return m_sender; };

   /**
    * @brief Set the send_date of object
    *
    * @param send_date The send_date of object
    */
   void setSendDate(const std::time_t &send_date) { m_send_date = send_date; };

   /**
    * @brief Get the send_date of object
    *
    * @return const std::time_t& the send_date of object
    */
   const std::time_t &getSendDate() const { return m_send_date; };

   /**
    * @brief Set the chat of object
    *
    * @param chat The chat of object
    */
   void setChat(const std::shared_ptr<chat> &chat) { m_chat = chat; };

   /**
    * @brief Get the chat of object
    *
    * @return const std::shared_ptr<chat>& the chat of object
    */
   const std::shared_ptr<chat> &getChat() const { return m_chat; };

 };

 #pragma db object(message)

如果你能解决这个问题,非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2022-09-06 11:23:40

当您对ORM类执行前向声明时,应该告诉ODB编译器该类的定义,如下例所示。

  • chat.hxx
代码语言:javascript
复制
#pragma once
...
class message;
...
#pragma db object ...
class chat ... {
...
    #pragma db ...
    std::vector<std::weak_ptr<message>> m_messages;
...
};

#ifdef ODB_COMPILER
#include "message.hxx"
#endif

有关细节,请参见参考文献

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73615402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档