gpt4 book ai didi

macos - 在 macos 上调用私有(private)构造函数时出错

转载 作者:行者123 更新时间:2023-12-02 10:58:26 31 4
gpt4 key购买 nike

我正在尝试将一些将给定数据编码为base64的C++代码移植到macos。这是来自同一来源的代码示例,它在 Godbolt 上编译和执行,但不在我的 mac 上:

#include <vector>
#include <string>

#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>

using namespace boost::archive::iterators;

struct Encode
{
/// Sets the contents of an std::string to be used
/// as the input for the encoding operation.
/// \param a_Data The string to encode.
Encode(const std::string& a_Data);

/// Evaluates the expression and performs the base64 encoding.
/// \returns Base64 encoded string.
operator std::string() const;
private:
std::string Evaluate() const;

const char * m_Data;
size_t m_Size;
bool m_LineBreaks;
};

Encode::Encode(const std::string& a_Data)
: m_Data(a_Data.c_str())
, m_Size(a_Data.size())
, m_LineBreaks(false)
{
}

std::string Encode::Evaluate() const
{
typedef base64_from_binary<
transform_width<std::string::const_iterator,6,8>
> iterator;

typedef insert_linebreaks<iterator, 72> linebreak_iterator;

std::string base64;
if (m_LineBreaks) {
base64.assign(
linebreak_iterator(m_Data),
linebreak_iterator(m_Data + m_Size));
}
else {
base64.assign(
iterator(m_Data),
iterator(m_Data + m_Size));
}

return base64;
}
Encode::operator std::string() const
{
return Evaluate();
}

int main()
{
const std::string str64 = Encode("Hello World");
return 0;
}

我正在使用 g++ 进行编译:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

但是, iterator(m_Data), 行出现以下错误:
boost/archive/iterators/transform_width.hpp:112:17: error: calling a private constructor of class 'std::__1::__wrap_iter<const char *>'
super_t(Base(static_cast< T >(start))),
^
<build_path>/boost/archive/iterators/base64_from_binary.hpp:91:13: note: in instantiation of function template specialization 'boost::archive::iterators::transform_width<std::__1::__wrap_iter<const char *>, 6, 8,
char>::transform_width<const char *>' requested here
Base(static_cast< T >(start)),
^
<src_path>/utilsBase64.cc:105:13: note: in instantiation of function template specialization 'boost::archive::iterators::base64_from_binary<boost::archive::iterators::transform_width<std::__1::__wrap_iter<const char *>, 6, 8, char>, char>::base64_from_binary<const
char *>' requested here
iterator(m_Data),
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1420:31: note: declared private here
_LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT_DEBUG : __i(__x) {}
^

我尝试使用 clang++ 并尝试设置 -stdlib=libc++stdlib=libstdc++有类似的结果。代码中显示的所有使用 base64_from_binary 的示例都与给定的代码示例类似。

关于 m_Data 的一个附加细节,它是一个声明为的成员变量: const char * m_Data;
有人可以解释如何解决这个问题吗?

最佳答案

将代码修改为以下有助于解决问题:

std::string Encode::Evaluate() const
{
typedef base64_from_binary<
transform_width<std::string::const_iterator,6,8>
> iterator;

typedef insert_linebreaks<iterator, 72> linebreak_iterator;

std::string data(m_Data, m_Size);
std::string base64;
if (m_LineBreaks) {
base64.assign(
linebreak_iterator(data.begin()),
linebreak_iterator(data.end()));
}
else {
base64.assign(
iterator(data.begin()),
iterator(data.end()));
}

return base64;
}

关于macos - 在 macos 上调用私有(private)构造函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52744134/

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