1

i'm using gcc 4.6.2.

I'm trying to push_back in a vector shared_ptr's.

But gcc gives me everytime an error.

Here my codelines:

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;

The Error my gcc is giving is:

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>&&}'

In my eyes the error say's, that std::make_shared create a std::shared_ptr.
But in gcc shared_ptr is in the namespace std::tr1.
How could i fix it?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
demonking
  • 2,584
  • 5
  • 23
  • 28
  • `std::make_shared(x...)` is equivalent to `std::shared_ptr(new T(x...))` (without unnecessary ref-counts), so you should just call `std::make_shared(procName, procInputs, procInstructions)`. – kennytm Dec 06 '11 at 20:06

2 Answers2

9

If I understand correctly, make_shared is new in C++11 and is in namespace std, but it is only available if you compile with -std=gnu++0x or similar. But if you do that, then shared_ptr is also in std.

The problem is that there is another version of shared_ptr in std::tr1, but in C++11 mode you should not use it: it should be considered deprecated.

Your solution is simply to remove every use of tr1 and use the full C++11 version of these classes.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
3

C++ template error message can be a beast to read. But the answer is in the 2nd 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>&}'

The problem is you're using std::make_shared (which creates a std::shared_ptr) and passing it into a vector of std::tr1::shared_ptr.

The simplest solution is drop the TR1. The stuff from the TR1 was some of first features implemented by compilers when adding C++11 support.

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

If you are unable to stop using std::tr1::shared_ptr. You'll have to forgo using make_shared as it was not part of the TR1.

deft_code
  • 57,255
  • 29
  • 141
  • 224
  • I have written my own make_shared's in toy projects that needed to work with tr1 libraries. Is there any pitfall in doing so? – cardiff space man Jun 05 '13 at 17:16
  • `make_shared` is more than a wrapper on the `shared_ptr` ctor. It relies on internal details to put the metadata with the type in one allocation. You need to ensure your version is compatible both implementations, which wont be portable between anything (compilers, compiler version, days of the week). – deft_code Jun 08 '13 at 14:41
  • Thanks. I wouldn't expect compatibility down to such details, just convenience when not always using a recently-released compiler. – cardiff space man Jun 10 '13 at 06:28