使用c ++ 11构造unique_ptr

时间:2018-02-01 19:43:41

标签: c++ c++11 pointers unique

我有一些代码:

#include <string>
#include <memory>
#include <queue>

template <typename T>
class MyQueue {
  public:
  MyQueue() {
    q_(new std::queue<T>);
  }

  private:
  std::unique_ptr<std::queue<T>> q_; 
};

int main() {
  MyQueue<std::string> queue;
  return 0;
}

当我用g ++编译(-std = c ++ 11)时,我得到一个输出,表明我没有正确构造我的unique_ptr。什么是适当的&#39;构建这样一个unique_ptr的方法?

输出:

test.cc: In instantiation of ‘MyQueue<T>::MyQueue() [with T = 
    std::__cxx11::basic_string<char>]’:
test.cc:18:24:   required from here
test.cc:9:5: error: no match for call to 
    ‘(std::unique_ptr<std::queue<std::__cxx11::basic_string<char>, 
std::deque<std::__cxx11::basic_string<char>, 
std::allocator<std::__cxx11::basic_string<char> > > >, 
std::default_delete<std::queue<std::__cxx11::basic_string<char>, 
std::deque<std::__cxx11::basic_string<char>, 
std::allocator<std::__cxx11::basic_string<char> > > > > >) 
(std::queue<std::__cxx11::basic_string<char>, 
std::deque<std::__cxx11::basic_string<char>, 
std::allocator<std::__cxx11::basic_string<char> > > >*)’
     q_(new std::queue<T>);
     ^

0 个答案:

没有答案