没有用于'vector< string>的初始化的匹配构造函数与clang ++ 3.2 [英] no matching constructor for initialization of 'vector<string>' with clang++ 3.2

查看:1471
本文介绍了没有用于'vector< string>的初始化的匹配构造函数与clang ++ 3.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++ Primer学习c ++,第5版。

I'm learning c++ with C++ Primer, 5th ed..

我试图用clang ++编译一个简单的c ++程序,我收到了应该是有效代码的编译错误。

I'm trying to compile a simple c++ program with C++11 features with clang++ but I'm getting compile errors for what should be valid code.

这是一个例子:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(){
  int n = 0;
  auto *p = &n; //<-- this compiles  

  cout << *p << endl;

  vector<string> articles = {"a", "an", "the"}; //<-- this fails; copied from the book

  return 0;
}

这是完整的错误:

$ clang++ -std=c++11 -v test.cpp -o test
clang version 3.2 (tags/RELEASE_32/final)
Target: x86_64-apple-darwin10.8.0
Thread model: posix
 "/usr/local/Cellar/llvm/3.2/bin/clang" -cc1 -triple x86_64-apple-macosx10.6.0 -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 127.2 -v -resource-dir /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2 -fmodule-cache-path /var/folders/pk/pkIeYbRaF-yeeUH3Q6Q5AE+++TI/-Tmp-/clang-module-cache -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/Oton/Desktop -ferror-limit 19 -fmessage-length 150 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.6.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/pk/pkIeYbRaF-yeeUH3Q6Q5AE+++TI/-Tmp-/test-w65CaF.o -x c++ test.cpp
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-apple-darwin10.8.0
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2.1
 /usr/include/c++/4.2.1/i686-apple-darwin10/x86_64
 /usr/include/c++/4.2.1/backward
 /usr/include/c++/4.0.0
 /usr/include/c++/4.0.0/i686-apple-darwin8
 /usr/include/c++/4.0.0/backward
 /usr/local/include
 /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
test.cpp:13:18: error: no matching constructor for initialization of 'vector<string>'
  vector<string> articles = {"a", "an", "the"};
                 ^          ~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.2.1/bits/stl_vector.h:255:9: note: candidate constructor [with _InputIterator = const char *] not viable: no known conversion from
      'const char [4]' to 'const allocator_type' (aka 'const std::allocator<std::basic_string<char> >') for 3rd argument
        vector(_InputIterator __first, _InputIterator __last,
        ^
/usr/include/c++/4.2.1/bits/stl_vector.h:213:7: note: candidate constructor not viable: no known conversion from 'const char [2]' to 'size_type'
      (aka 'unsigned long') for 1st argument
      vector(size_type __n, const value_type& __value = value_type(),
      ^
/usr/include/c++/4.2.1/bits/stl_vector.h:201:7: note: candidate constructor not viable: allows at most single argument '__a', but 3 arguments were
      provided
      vector(const allocator_type& __a = allocator_type())
      ^
/usr/include/c++/4.2.1/bits/stl_vector.h:231:7: note: candidate constructor not viable: requires single argument '__x', but 3 arguments were provided
      vector(const vector& __x)
      ^
1 error generated.

我在mac上,10.6.8,我使用homebrew创建了llvm / clang选项:

I'm on a mac, 10.6.8, I built llvm/clang with homebrew with options:

brew install llvm -v --all-targets --rtti --shared --with-asan --with-clang --use-clang

我做错了什么?

推荐答案

错误告诉你没有std :: vector构造函数接受初始化列表。问题是,您正在使用的标准库实现没有更新为C ++ 11。您使用的是libstdc ++的gcc 4.2版本,它是在C ++ 11之前几年编写的。

The error tells you that there's no std::vector constructor taking an initializer list. The problem is that the standard library implementation you are using has not been updated for C++11. You are using the gcc 4.2 version of libstdc++, which was written years before C++11.

您必须切换到已更新为C ++的库11。如果你安装了libc ++,你可以使用 -stdlib = libc ++ 标志。我认为libc ++二进制是OS X 10.6.8的一部分,但你也需要标头。也许homebrew有一个食谱,或者你可以从下载它们: http://libcxx.llvm.org/ 并将它们粘贴到适当的位置,找到并使用 -stdlib = libc ++

You have to switch to a library that has been updated for C++11. If you have libc++ installed then you can do just use the flag -stdlib=libc++. I think the libc++ binary is a part of OS X 10.6.8, but you'll also need headers. Maybe homebrew has a recipe, or you can just download them from: http://libcxx.llvm.org/ and stick them in the appropriate location to be found found and used with -stdlib=libc++.

-nostdinc ++ 中手动删除标准的C ++标准库头文件(在这种情况下为gcc 4.2),手动添加一个include目录,搜索目录,无论二进制是什么(你甚至可以自己构建二进制),并添加链接标志 -lc ++

Alternatively you can manually leave out the normal C++ standard library headers (the gcc 4.2 ones in this case) with -nostdinc++, manually add an include directory for wherever you stick the headers, add a library search directory for wherever the binary is (you can even build the binary yourself) and add the link flag -lc++.

这篇关于没有用于'vector&lt; string&gt;的初始化的匹配构造函数与clang ++ 3.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆