C++ std::auto_ptr在我的模板类中编译,但不是std::unique_ptr

C++ std::auto_ptr在我的模板类中编译,但不是std::unique_ptr,c++,templates,c++11,unique-ptr,auto-ptr,C++,Templates,C++11,Unique Ptr,Auto Ptr,我启动了一个模板类,该类应该管理一个固定长度的deque。我想添加一个函数,返回转换成向量的数据。因为我不能确定这将使用良好的NRVO(名为返回值优化)进行编译,并且理论上数据可能相当大,所以我决定将返回包装在一个唯一的_ptr中(以避免在最后大量调用复制构造函数)。奇怪的是,这并没有编译: In file included from FixedDeque.cpp:8: FixedDeque.h:26: error: ISO C++ forbids declaration of 'uniqu

我启动了一个模板类,该类应该管理一个固定长度的deque。我想添加一个函数,返回转换成向量的数据。因为我不能确定这将使用良好的NRVO(名为返回值优化)进行编译,并且理论上数据可能相当大,所以我决定将返回包装在一个唯一的_ptr中(以避免在最后大量调用复制构造函数)。奇怪的是,这并没有编译:

 In file included from FixedDeque.cpp:8:
 FixedDeque.h:26: error: ISO C++ forbids declaration of 'unique_ptr' with no type
 FixedDeque.h:26: error: invalid use of '::'
 FixedDeque.h:26: error: expected ';' before '<' token
 FixedDeque.cpp:27: error: expected constructor, destructor, or type conversion before '<' token
FixedEQUE.cpp中包含的文件中的
:8:
FixDeG.H:26:错误:ISO C++禁止声明“UNQuyJ-PTR”,没有类型
FixedEQUE.h:26:错误:对“:”的使用无效

FixedDequee.h:26:错误:应为“;”在“之前,您的代码基本上是正确的,只是您不能编译类模板。要使其工作,必须在头文件中声明并实现类模板,包括头文件并在程序中实例化类模板。请参见下面的示例:

// This is FixedDeque.hpp
#include <iostream>
#include <deque>
#include <vector>
#include <memory>

template<typename T> class FixedDeque {
public:
  FixedDeque(int size);
  FixedDeque(const FixedDeque<T>& orig);
  virtual ~FixedDeque();
  // adding an auto_ptr/unique_ptr because not sure the C++ NRVO is applied in the compiler
  std::unique_ptr< std::vector<T> > getVectorCopy() const;
private:
  std::deque<T> _deq;
  int _maxSize;
};

template<typename T> FixedDeque<T>::FixedDeque(int size)  : _deq(size), _maxSize(size)
{ }
template<typename T> FixedDeque<T>::FixedDeque(const FixedDeque<T>& orig) : _deq(orig._deq) {}
template<typename T> FixedDeque<T>::~FixedDeque() {}  
template<typename T> std::unique_ptr< std::vector<T> > FixedDeque<T>::getVectorCopy() const
{
    std::vector<T>* apVector = new std::vector<T>();
    return( std::unique_ptr< std::vector<T> >(apVector) );
}  

// This is FixedDeque_Test.cpp
int main() {
    FixedDeque<int> fdeque(10);
    std::unique_ptr<std::vector<int>> vec = fdeque.getVectorCopy();
    std::cout << vec->size() << std::endl;
    return 0;
}
//这是fixedEQUE.hpp
#包括
#包括
#包括
#包括
模板类fixedEQUE{
公众:
FixedEQUE(整数大小);
固定数量(常量固定数量和原始数量);
虚拟~fixedEQUE();
//添加AutoYPTR/UnQuyQPTR,因为不确定编译器中应用C++ NRVO
std::unique_ptrgetVectorCopy()常量;
私人:
std::deque _deq;
int_maxSize;
};
模板fixedEQUE::fixedEQUE(整数大小):\u deq(大小),\u maxSize(大小)
{ }
模板fixedEQUE::fixedEQUE(常量fixedEQUE&orig):\u deq(orig.\u deq){}
模板fixedEQUE::~fixedEQUE(){}
模板std::unique_ptrfixedEQUE::getVectorCopy()常量
{
std::vector*apVector=new std::vector();
返回(std::unique_ptr(apVector));
}  
//这是FixedDeque_Test.cpp
int main(){
固定设备fdeque(10);
std::unique_ptr vec=fdeque.getVectorCopy();

C++:C++ C++版本,你有没有启用C++代码??你是否启用了<代码> -STD= C++ 11?/代码>?应该是<代码> >包含< /代码>。你使用C++ 11编译器吗?我使用的是NETBea7.3.1,它应该有C++ 11作为标准。我在C++编译器下把C++标准设置成C++ 11。对于项目属性,它给了我:cc1plus:error:unrecogned命令行选项“-std=c++11”。因此,看起来我可能必须升级我的g++版本。我从苹果的XCode 3.x(最新的雪豹兼容版本)获得了它-所以可能需要升级g++。嗨,迭戈,非常感谢你的快速回复。这看起来肯定是个编译器问题。如果你可以编译我的模板,那么你肯定可以使用它,我遇到的问题是它拒绝编译,除非我使用auto_ptr而不是unique_ptr。我复制并粘贴了你的代码,我仍然得到了同样的问题(雪豹上的g++)。如果您的意思是我可以将模板函数实现与声明放在同一个文件中,我也这样做了。谢谢您试一试。@user2835640就像其他人说的那样,您需要启用编译器对c++11的支持,以使用std::unique\u ptr。@user2835640好的,在Snow Leopard上,您可能没有c++11编译器。您不能吗ry g++-std=c++11 fixedEQUE.cpp-o fixedEQUE?您也可以尝试使用clang,比如:clang++-std=c++11-stdlib=libc++fixedEQUE.cpp-o fixedEQUE。请在编译之前将我的答案中的全部代码放在一个文件中。Diego,drescherjm,感谢您的帮助。正如我在上面的评论中提到的,我可能需要找到一种方法来升级m在雪豹上没有任何其他的东西,我会讨论这个决定,但是现在我会考虑这个问题。这是一个有趣的错误(使用UNIQuyPPTR模板在非C++ 11环境中)。谢谢大家-看起来是一个非常有用的帮助网站。我还将尝试安装clang编译器。@user2835640尝试将您的XCode更新到最新版本。它将带来一个支持C++11的更新的clang,您将能够毫无问题地编译它。
// This is FixedDeque.hpp
#include <iostream>
#include <deque>
#include <vector>
#include <memory>

template<typename T> class FixedDeque {
public:
  FixedDeque(int size);
  FixedDeque(const FixedDeque<T>& orig);
  virtual ~FixedDeque();
  // adding an auto_ptr/unique_ptr because not sure the C++ NRVO is applied in the compiler
  std::unique_ptr< std::vector<T> > getVectorCopy() const;
private:
  std::deque<T> _deq;
  int _maxSize;
};

template<typename T> FixedDeque<T>::FixedDeque(int size)  : _deq(size), _maxSize(size)
{ }
template<typename T> FixedDeque<T>::FixedDeque(const FixedDeque<T>& orig) : _deq(orig._deq) {}
template<typename T> FixedDeque<T>::~FixedDeque() {}  
template<typename T> std::unique_ptr< std::vector<T> > FixedDeque<T>::getVectorCopy() const
{
    std::vector<T>* apVector = new std::vector<T>();
    return( std::unique_ptr< std::vector<T> >(apVector) );
}  

// This is FixedDeque_Test.cpp
int main() {
    FixedDeque<int> fdeque(10);
    std::unique_ptr<std::vector<int>> vec = fdeque.getVectorCopy();
    std::cout << vec->size() << std::endl;
    return 0;
}