每当我尝试使用接受任何参数的构造函数时,都会收到编译器错误“没有匹配的函数来调用'make_shared'”。因此,例如:
std::shared_ptr<int> foo = std::make_shared<int> ();
工作良好。但,
std::shared_ptr<int> foo = std::make_shared<int> (10);
给出以下错误:
/usr/bin/clang -g -Wall -Wextra -Wc++11-extensions -c ./test.cpp
./test.cpp:7:30: error: no matching function for call to 'make_shared'
std::shared_ptr<int> foo = std::make_shared<int> (10);
^~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4700:1: note: candidate function [with _Tp = int, _A0 = int]
not viable: expects an l-value for 1st argument
make_shared(_A0& __a0)
我直接从此处http://www.cplusplus.com/reference/memory/make_shared/获取了上述代码,该代码在cpp.sh网站上运行良好。我怀疑编译器设置有问题。在Macbook上的iTerm中运行。另外,即使我删除了上面显示的clang的各种选项,我也会遇到相同的错误。有任何想法吗?我的头文件是否可能需要更新?它是从2015年9月4日开始的。似乎最近才足以使C ++ 11正常工作。
$ /usr/bin/clang --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
$ ls -l /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory
-rw-r--r-- 1 root wheel 174919 Sep 4 2015 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory
错误消息正在抱怨您使用prvalue 10
。尝试使用
int avar = 10;
auto foo = std::make_shared<int> (avar);
有趣的是,使用左值时会发生什么。
您是否在本地构建标准库?如果是这样,也许您可以尝试重新构建或从某个地方获取预构建的库。
我在https://godbolt.org/上使用configurationx86-64 clang 7.0.0
和测试了代码-std=c++11
。它工作正常。我猜即使是使用iOS的人也应该很好。
我还看到您-Wc++11-extensions
在构建时使用。尝试使用-std=c++11
代替吧?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句