我用的是gcc 4.6.2。
我正在尝试在一个向量push_back _ptr中共享。
但是每次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中。
我怎么才能修复它呢?
发布于 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版本。
发布于 2011-12-06 23:55:07
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的一部分。
https://stackoverflow.com/questions/8398540
复制相似问题