首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >make_shared是否创建std::shared_ptr?gcc 4.6.2

make_shared是否创建std::shared_ptr?gcc 4.6.2
EN

Stack Overflow用户
提问于 2011-12-06 18:41:19
回答 2查看 6.1K关注 0票数 1

我用的是gcc 4.6.2。

我正在尝试在一个向量push_back _ptr中共享。

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

下面是我的代码行:

代码语言:javascript
复制
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给出的错误是:

代码语言:javascript
复制
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中。

我怎么才能修复它呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-06 19:50:01

如果我理解正确的话,make_shared是C++11中的新特性,并且位于名称空间std中,但是只有在使用-std=gnu++0x或类似工具进行编译时,它才可用。但是如果你这样做了,那么shared_ptr也在std中。

问题是在std::tr1中还有另一个版本的shared_ptr,但是在C++11模式下不应该使用它:它应该被认为是不推荐使用的。

您的解决方案就是删除tr1的所有用法,并使用这些类的完整C++11版本。

票数 9
EN

Stack Overflow用户

发布于 2011-12-06 23:55:07

C++模板错误消息可以是一个难以阅读的东西。但答案在第二个音符中。

代码语言:javascript
复制
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支持时实现的首批特性之一。

代码语言:javascript
复制
std::vector< std::shared_ptr<Process> > procs;

如果您无法停止使用std::tr1::shared_ptr。您必须放弃使用make_shared,因为它不是TR1的一部分。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8398540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档