gpt4 book ai didi

c++ - boost::any 库不编译: "Array used as initializer"错误

转载 作者:行者123 更新时间:2023-11-30 01:16:43 25 4
gpt4 key购买 nike

我正在使用 boost::any (以及其他 Boost 功能)在 C++ 项目中。

以下代码在我的 Mac(运行最新版本 Mavericks 的 MacBook Pro Retina)上使用 g++ 编译得很好:

#include <boost/any.hpp>

但是当我使用相同的编译设置/标志将 Ubuntu Linux 与 g++ 结合使用时,出现以下错误:

In file included from /home/alexandergunnarson/Documents/Source Code/byu/library/collections.cpp:11:0,
from /home/alexandergunnarson/Documents/Source Code/byu/library/collections.h:4,
from /home/alexandergunnarson/Documents/Source Code/byu/library/main.cpp:16:
/usr/include/boost/any.hpp: In instantiation of ‘boost::any::holder<ValueType>::holder(const ValueType&) [with ValueType = char [5]]’:
/usr/include/boost/any.hpp:52:49: required from ‘boost::any::any(const ValueType&) [with ValueType = char [5]]’
/home/alexandergunnarson/Documents/Source Code/byu/library/main.cpp:149:22: required from here
/usr/include/boost/any.hpp:169:27: error: array used as initializer
: held(value)
^

collections.cpp:11:0指的是#include <boost/any.hpp>声明。

我在 Ubuntu 14.04 上使用来自 Sublime Text 3 的 g++ 4.9。

这是 Boost 问题还是什么?

感谢您的帮助!

更新:

作为 T.C.使用他的“通灵能力”进行预测,我试图将字符串文字转换为 boost::any .现在代码在经过并将一些字符串文字放入 string 后工作。构造函数并编辑了一些相关函数。

最佳答案

我的通灵能力表明您正试图将字符串文字放入 boost::any 中。这是不允许的;字符串文字是 const char 的数组,并且数组不是 CopyConstructibleboost::any 需要。

代替

boost::any t("foo");

使用

boost::any t(+"foo");

强制数组衰减为指针,或者

boost::any t(std::string("foo")); // or "foo"s in C++14

让它存储一个std::string

关于c++ - boost::any 库不编译: "Array used as initializer"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25943650/

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