Daniel Goldfarb Daniel Goldfarb - 1 month ago 8
C++ Question

Compiler complains make_shared() expects l-value

I have a simple class. One of its constructors takes two ints as arguments:

Dino@MacDino:~/code/test$ cat simple_class.h

class SimpleClass {
private:
int d_ii;
int d_jj;
public:
SimpleClass() : d_ii(40), d_jj(10) {}
SimpleClass( const int ii, const int jj ) : d_ii(ii), d_jj(jj) {}
};


Dino@MacDino:~/code/test$ cat test.t.cpp

//----------------------------------------------------------------//
//
// Test Program
#include <memory>
#include <simple_class.h>

int main ( int argc, char * argv[] )
{
SimpleClass sc1;
SimpleClass sc2( 10, 20 );

std::shared_ptr<SimpleClass> spSc1( new SimpleClass(10,12) );

int ii = 10;
int jj = 16;
std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass> ( ii, jj );

std::shared_ptr<SimpleClass> spSc3 = std::make_shared<SimpleClass> ( 10, 16 );

return 0;
}


Running on a macbook. Here is my compile:


Dino@MacDino:~/code/test$ gcc -o test -I. test.t.cpp -lstdc++

test.t.cpp:18:41: error: no matching function for call to 'make_shared'
std::shared_ptr spSc3 = std::make_shared ( 10, 16 );
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4708:1: note:
candidate function [with _Tp = SimpleClass, _A0 = int, _A1 = int] not viable: expects an l-value for 1st
argument
make_shared(_A0& __a0, _A1& __a1)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4692:1: note:
candidate function template not viable: requires 0 arguments, but 2 were provided
make_shared()
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4700:1: note:
candidate function template not viable: requires single argument '__a0', but 2 arguments were provided
make_shared(_A0& __a0)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4716:1: note:
candidate function template not viable: requires 3 arguments, but 2 were provided
make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
^
1 error generated.


So the question is, why doesn't this usage of make_shared work?

std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass>( 10, 16 );


Notice that the version with the l-values passed does compile:

int ii = 10;
int jj = 16;
std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass> ( ii, jj );


Can anyone explain why this is? My constructor declares the arguments as const (although that should not be necessary). And the examples given here http://www.cplusplus.com/reference/memory/make_shared/ pass constants to make_shared. Any ideas? Thanks.

Answer

You are compiling C++ code with a C compiler, use g++ instead. Also, std::shared_ptr and relatives are C++11 features, which need to be enabled on G++ compiler. So your command line would be g++ -o test -I. test.t.cpp -std=c++11 (it is not needed to link against libstdc++, g++ does it automatically)

edit: Also, G++ 4.2 does not support C++11 features. On OSX, use clang++ instead