使用gcc 4.6的模板类的shared_ptr

rkb*_*rkb -1 c++ templates c++11

试图在c ++ 11中实现以下功能

template< class A >
    class B{
         std::shared_ptr< A > a_shared_ptr;
    };

B< int > b;
Run Code Online (Sandbox Code Playgroud)

可能吗?

获得以下错误

../../src/threading/node.h:26:错误:ISO C++禁止声明'shared_ptr'没有类型../../src/threading/node.h:26:错误:无效使用'::'../../src/threading/node.h:26:错误:预期';' 在'<'标记之前

Pie*_*aud 6

是的,这是可能的.

作为std::shared_ptr新的C++ 11特性,您必须在编译器上启用C++ 11支持.根据海湾合作委员会,选项是:-std=c++0x-std=gnu++0x.

如果我不启用这些功能,我会得到与您完全相同的错误.

另一点是:不要忘记包括标题std::shared_ptr:

#include <memory>
Run Code Online (Sandbox Code Playgroud)


apo*_*kul 5

只需包含std :: shared_ptr的头文件,它编译好了:

#include <memory>

template< class A >
class B{
    std::shared_ptr< A > a_shared_ptr;
};

int main()
{
    B< int > b;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)