C++ C++;11-如何将此对象推入具有共享\u ptr向量的优先级\u队列?

C++ C++;11-如何将此对象推入具有共享\u ptr向量的优先级\u队列?,c++,c++11,vector,shared-ptr,priority-queue,C++,C++11,Vector,Shared Ptr,Priority Queue,我有一个基类,它有一个优先级队列,如下所示: class base { //... std::priority_queue<std::shared_ptr<Obj>, std::vector<std::shared_ptr<Obj>>, obj_less> obj_queue; //... } 这个set()将调用我的基类上的set(): void base::set(const Obj& o) { obj_qu

我有一个
基类
,它有一个
优先级队列
,如下所示:

class base
{
   //...
   std::priority_queue<std::shared_ptr<Obj>, std::vector<std::shared_ptr<Obj>>, obj_less> obj_queue;
   //...
}
这个
set()
将调用我的
基类上的
set()

void base::set(const Obj& o)
{
    obj_queue.push(o);
}
我想使用
this
,获取指向同一
Obj
的指针,并将其推入我的
优先级队列中的
向量中

但它甚至无法编译,我有点迷路了…

你知道我遗漏了什么吗

myObj.set(this);
传递一个指针,但是

void base::set(const Obj& o)
需要一个对象

传递一个指针,但是

void base::set(const Obj& o)
需要一个对象


实际上,您不应该这样做,因为这是一个非常糟糕的主意,只有当您在
Obj
上有原始指针而不是调用
set
函数时,您才会有问题。您的代码的想法很奇怪,但是,实际上最好使用
shared\u ptr
从这个
中启用\u shared\u

class Obj : public std::enable_shared_from_this<Obj>
{
public:
   // ...
   void set()
   {
      BaseServer& myObj = BaseFactory::getBase();
      myObj.set(std::shared_from_this()); //<------ won't compile :(
   }
};

实际上,您不应该这样做,因为这是一个非常糟糕的主意,只有当您在
Obj
上有原始指针而不是调用
set
函数时,您才会有问题。您的代码的想法很奇怪,但是,实际上最好使用
shared\u ptr
从这个
中启用\u shared\u

class Obj : public std::enable_shared_from_this<Obj>
{
public:
   // ...
   void set()
   {
      BaseServer& myObj = BaseFactory::getBase();
      myObj.set(std::shared_from_this()); //<------ won't compile :(
   }
};

可能您需要
myObj.set(*this)
void base::set(const Obj*o)
将错误添加到问题中可能需要
myObj.set(*此)
void base::set(const Obj*o)
。将错误添加到问题中
class Obj : public std::enable_shared_from_this<Obj>
{
public:
   // ...
   void set()
   {
      BaseServer& myObj = BaseFactory::getBase();
      myObj.set(std::shared_from_this()); //<------ won't compile :(
   }
};
class Obj : public std::enable_shared_from_this<Obj>
{
private:
   Obj() {}
public:
   static std::shared_ptr<Obj> create()
   {
      return std::make_shared<Obj>();
   }
   // rest code
};

// code, that calls set function
auto object = Obj::create();
object->set();