gpt4 book ai didi

C++11 g++ 带有大括号括起来的初始化列表

转载 作者:行者123 更新时间:2023-11-30 04:03:20 25 4
gpt4 key购买 nike

我是编程的新手,所以如果这是一个明显的问题,我深表歉意,但是我在一本书(C++ 的创建者 Bjarne Stroustrup 使用 C++ 第二版的编程原理和实践)中遇到了语法问题).其中介绍了创建字符串 vector 的以下内容:

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)

有什么地方出错了吗?

非常感谢您的帮助。

最佳答案

你在使用这个 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 。宏定义有问题并且不关注 namespace ,因此 std::vector 将成为不存在的 std::Vector

注2:T.C.正确地评论说 string 也存在类似的问题:请改用 std::string

关于C++11 g++ 带有大括号括起来的初始化列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24499569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com