编译器报错make_shared()期望左值
Compiler complains make_shared() expects l-value
我有一个简单的类。它的一个构造函数接受两个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 -Itest.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
(尽管这应该是不必要的)。这里给出的例子将常量传递给make_shared()
。
您正在用C编译器编译c++代码,请使用g++
代替。另外,std::shared_ptr
及其相关项是c++ 11的特性,需要在c++编译器上启用。所以你的命令行应该是g++ -o test -I. test.t.cpp -std=c++11
(不需要链接libstdc++, g++会自动链接)
另外,c++ 4.2不支持c++ 11的特性。在OSX上,使用clang++
代替
相关文章:
- 从类型std::函数传递变量失败,尽管调用方期望的类型完全相同
- 一元*运算符的操作数是否期望一个 prvalue
- 什么 /可以/ 你从一个函数返回. 并期望它在到达时还活着?
- 引用 std::shared:ptr 以避免引用计数
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- 根据实例化点期望不同的类型
- gtest 期望无序元素与自定义比较器/匹配器一起使用
- C++ 期望":"(冒号)的构造函数
- 将 std::vector::d ata 传递给函数期望类型**(双指针)
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- 期望在奇怪的地方有一个";"
- 如何期望通过使用Google Test(Mock)以特定频率调用函数
- 如何测试序列生成器将生成期望的项目
- 提升精神 x3 错误处理程序与期望
- 无法使用 libtool 将 -shared 参数传递给 g++
- C++中的后缀不像我期望的那样表现
- MSVC++ 17 std::copy 期望自定义迭代器的"operator -"
- 如何区分在同一行上创建的 gmock 期望
- 为什么我的 Hippomock 期望在使用多重继承时失败
- std::设置扣除指南不像我期望的那样工作