1

私は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 にあります。
どうすれば修正できますか?

4

2 に答える 2

9

私が正しく理解していれば、make_sharedは C++11 の新機能であり、 namespaceにありますが、または類似のものstdでコンパイルした場合にのみ使用できます。-std=gnu++0xしかし、それを行うと、shared_ptrstd.

問題は、別のバージョンのshared_ptrinがあることですstd::tr1が、C++11 モードでは使用しないでください。非推奨と見なす必要があります。

あなたの解決策は、これらのクラスのすべての使用を削除してtr1、完全な C++11 バージョンを使用することです。

于 2011-12-06T11:50:01.487 に答える
3

C++ テンプレートのエラー メッセージは、非常に読みにくいものです。しかし、答えは2番目のメモにあります。

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_ptrmake_sharedTR1 の一部ではなかったので、使用を控える必要があります。

于 2011-12-06T15:55:07.383 に答える