编译器抱怨 make_shared() 期望左值
Compiler complains make_shared() expects l-value
我有一个简单的class。它的构造函数之一将两个 int
值作为参数:
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) {}
};
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;
}
运行 在 macbook 上。这是我的编译语句:
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<SimpleClass> spSc3 = std::make_shared<SimpleClass> ( 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.
所以我的问题是,为什么 make_shared()
的这种用法不起作用?
std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass>( 10, 16 );
注意左值通过的版本确实编译:
int ii = 10;
int jj = 16;
std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass> ( ii, jj );
谁能解释这是为什么?
我的构造函数将参数声明为 const
(尽管这不是必需的)。 examples given here 将常量传递给 make_shared()
.
您正在使用 C 编译器编译 C++ 代码,请改用 g++
。此外,std::shared_ptr
和相关项是 C++11 功能,需要在 G++ 编译器上启用。所以你的命令行将是 g++ -o test -I. test.t.cpp -std=c++11
(不需要 link 反对 libstdc++,g++ 会自动执行)
编辑:
此外,G++ 4.2 不支持 C++11 功能。在 OSX 上,使用 clang++
代替
我有一个简单的class。它的构造函数之一将两个 int
值作为参数:
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) {}
};
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;
}
运行 在 macbook 上。这是我的编译语句:
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<SimpleClass> spSc3 = std::make_shared<SimpleClass> ( 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.
所以我的问题是,为什么 make_shared()
的这种用法不起作用?
std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass>( 10, 16 );
注意左值通过的版本确实编译:
int ii = 10;
int jj = 16;
std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass> ( ii, jj );
谁能解释这是为什么?
我的构造函数将参数声明为 const
(尽管这不是必需的)。 examples given here 将常量传递给 make_shared()
.
您正在使用 C 编译器编译 C++ 代码,请改用 g++
。此外,std::shared_ptr
和相关项是 C++11 功能,需要在 G++ 编译器上启用。所以你的命令行将是 g++ -o test -I. test.t.cpp -std=c++11
(不需要 link 反对 libstdc++,g++ 会自动执行)
编辑:
此外,G++ 4.2 不支持 C++11 功能。在 OSX 上,使用 clang++
代替