我是编程新手,所以我很抱歉这是一个显而易见的问题,但是我在一本书(C ++的创建者使用C ++第二版编程原理和实践, Bjarne Stroustrup)。其中,以下内容用于创建字符串向量:
vector<string> philosopher
={"Kant","Plato","Hume","Kierkegaard"};
然而,当通过g ++传递它时,它并不喜欢它。我的代码如下:
#include "std_lib_facilities.h" //The author's library for his examples
int main()
{
vector<string>philosopher
={"Kant","Plato","Hume","Kierkegaard"};
}
我收到错误编译:
g++ vecttest.cpp -std=c++11
In file included from /usr/local/include/c++/4.9.0/ext/hash_map:60:0,
from /usr/include/std_lib_facilities.h:34,
from vecttest.cpp:1:
/usr/local/include/c++/4.9.0/backward/backward_warning.h:32:2: warning: #warning
This file includes at least one deprecated or antiquated header which may
be removed without further notice at a future date. Please use a
non-deprecated interface with equivalent functionality instead. For a listing
of replacement headers and interfaces, consult the file backward_warning.h.
To disable this warning use -Wno-deprecated. [-Wcpp]
#warning \
^
In file included from /usr/local/include/c++/4.9.0/locale:41:0,
from /usr/local/include/c++/4.9.0/iomanip:43,
from /usr/include/std_lib_facilities.h:212,
from vecttest.cpp:1:
/usr/local/include/c++/4.9.0/bits/locale_facets_nonio.h:1869:5: error:
template-id ‘do_get<>’ for
‘String std::messages<char>::do_get(std::messages_base::catalog, int, int,
const String&) const’ does not match any template declaration
messages<char>::do_get(catalog, int, int, const string&) const;
^
/usr/local/include/c++/4.9.0/bits/locale_facets_nonio.h:1869:62: note:
saw 1 ‘template<>’, need 2 for specializing a member function template
messages<char>::do_get(catalog, int, int, const string&) const;
^
vecttest.cpp: In function ‘int main()’:
vecttest.cpp:8:42: error: could not convert ‘{"Kant", "Plato",
"Hume","Kierkegaard"}’from ‘<brace-enclosed initializer list>’ to ‘Vector<String>’
={"Kant","Plato","Hume","Kierkegaard"};
我想也许我的GCC版本较旧(它的版本是4.7)所以我将其更新为4.9:
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.9.0/configure
Thread model: posix
gcc version 4.9.0 (GCC)
我出错的任何想法?
非常感谢你的帮助。
答案 0 :(得分:2)
您使用此std_lib_facilities.h
时出错了。 Looking at it online,它显示:
template< class T> struct Vector : public std::vector<T> {
...
};
// disgusting macro hack to get a range checked vector:
#define vector Vector
不幸的是,这个自定义Vector
模板类缺少std::vector
确实拥有的一些构造函数。
直接使用std::vector
即可使用。它在GCC 4.4和更新版本中得到了支持。
注意:要使用std::vector
,您需要确保根本不使用std_lib_facilities.h
,或使用#undef vector
。宏定义存在问题,并且不关注命名空间,因此std::vector
将成为不存在的std::Vector
。
注2:T.C。正确地评论string
也存在类似问题:改为使用std::string
。