【问题标题】:C++ string forward declaration is ambigiousC++ 字符串前向声明不明确
【发布时间】:2018-03-30 21:15:21
【问题描述】:

我尝试转发声明 std::string 但它似乎不起作用。

这是我的代码:

namespace std
{
    template<typename>
    class allocator;

    template<class _CharT>
    struct char_traits;

    template <typename _CharT, typename _Traits = char_traits<_CharT>, typename _Alloc = allocator<_CharT>>
    class basic_string;

    typedef basic_string<char> string;   
}

包含字符串会导致此错误:reference to ‘basic_string’ is ambiguous。有没有什么方法可以让 decleare 字符串没有错误?

【问题讨论】:

  • 不允许在std内转发声明名称
  • 不,see this 关于为什么这是不可能的,以及为什么会出现此错误。
  • 哦。好吧,当我包含“bits/stringfwd.hpp”时,它在 gcc 中工作,因此我认为我可以做到。
  • @DasOhmoffSan 作为一般规则,您永远不应该为您不拥有的类型编写前向声明。如果您想从库中转发声明某些内容,您希望库编写者为您提供转发声明文件,类似于标准库提供的 iosfwd 标头

标签: c++ string forward declare


【解决方案1】:

有什么方法可以无错误地声明 std::string 吗?

没有。

【讨论】:

  • Lightness Races in Orbit 责备我在 cmets 中提供完整和完整的答案。这是一个完整而完整的答案,尽管没有解释性的散文。 BobMorane 的链接提供了解释性散文。在我看来,这个问题应该作为重复关闭,并参考该链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
  • 2021-12-25
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2014-01-03
相关资源
最近更新 更多