簡體   English   中英

make_shared創建std :: shared_ptr嗎? gcc 4.6.2

[英]make_shared create std::shared_ptr? gcc 4.6.2

我正在使用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中。
我該如何解決?

如果我理解正確, make_shared在C ++ 11中是新的,並且在命名空間std ,但是只有在使用-std=gnu++0x或類似的-std=gnu++0x編譯時,它才可用。 但是,如果您這樣做,則shared_ptr也在std

問題在於std::tr1中還有shared_ptr另一個版本,但是在C ++ 11模式下,您不應使用它:應將其視為已棄用

您的解決方案就是刪除所有對tr1使用,並使用這些類的完整C ++ 11版本。

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的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM