make_shared创建std :: shared_ptr? gcc 4.6.2

时间:2011-12-06 10:41:19

标签: c++ c++11 shared-ptr gcc4 make-shared

我正在使用gcc 4.6.2。

我正在尝试使用矢量shared_ptr的push_back。

但是gcc每次都会给我一个错误。

这是我的代码行:

std::vector< std::tr1::shared_ptr<Process> > procs;
std::string line;
while (getline(file, line) && line.find(JobMask) != std::string::npos)
{
    std::string procName                      = line.substr(line.find(JobMask) + JobMask.size());
    std::vector<Instruction> procInstructions = extractProgram(file);
    std::queue<int>          procInputs       = extractInputs(file);

    if (!procInstructions.empty())
        procs.push_back(std::make_shared<Process>(Process(procName, procInputs, procInstructions))); //line 51
}
return procs;

我的gcc给出的错误是:

Process.cpp: In static member function 'static std::vector<std::tr1::shared_ptr<RMMIX::Process> > RMMIX::Process::createProcesses(const string&)':

Process.cpp:51:95: error: no matching function for call to 'std::vector<std::tr1::shared_ptr<RMMIX::Process> >::push_back(std::shared_ptr<RMMIX::Process>)'

Process.cpp:51:95: note: candidates are:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:826:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::tr1::shared_ptr<RMMIX::Process>, _Alloc = std::allocator<std::tr1::shared_ptr<RMMIX::Process> >, std::vector<_Tp, _Alloc>::value_type = std::tr1::shared_ptr<RMMIX::Process>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:826:7: note:   no known conversion for argument 1 from 'std::shared_ptr<RMMIX::Process>' to 'const value_type& {aka const std::tr1::shared_ptr<RMMIX::Process>&}'
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:839:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::tr1::shared_ptr<RMMIX::Process>, _Alloc = std::allocator<std::tr1::shared_ptr<RMMIX::Process> >, std::vector<_Tp, _Alloc>::value_type = std::tr1::shared_ptr<RMMIX::Process>]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:839:7: note:   no known conversion for argument 1 from 'std::shared_ptr<RMMIX::Process>' to 'std::vector<std::tr1::shared_ptr<RMMIX::Process> >::value_type&& {aka std::tr1::shared_ptr<RMMIX::Process>&&}'

在我眼中,错误说是,std :: make_shared创建了一个std :: shared_ptr。
但是在gcc中,shared_ptr位于命名空间std :: tr1中 我怎么能解决它?

2 个答案:

答案 0 :(得分:9)

如果我理解正确,make_shared在C ++ 11中是新的,并且在命名空间std中,但只有在使用-std=gnu++0x或类似编译时才可用。但是如果你这样做,那么shared_ptr也在std

问题是shared_ptr中有std::tr1的另一个版本,但在C ++ 11模式下,您不应该使用它:它应该被视为已弃用

您的解决方案只是删除tr1的每次使用,并使用这些类的完整C ++ 11版本。

答案 1 :(得分:3)

C ++模板错误信息可以是野兽阅读。但答案是在第二个注释中。

no known conversion for argument 1 from 'std::shared_ptr<RMMIX::Process>' to 'const value_type& {aka const std::tr1::shared_ptr<RMMIX::Process>&}'

问题是您正在使用std::make_shared(创建std::shared_ptr)并将其传递到std::tr1::shared_ptr的向量中。

最简单的解决方案是丢弃TR1。来自TR1的东西是编译器在添加C ++ 11支持时实现的一些首要功能。

std::vector< std::shared_ptr<Process> > procs;

如果您无法停止使用std::tr1::shared_ptr。您必须放弃使用make_shared,因为它不属于TR1。

相关问题