【问题标题】:ISO C++ says that these are ambiguous,ISO C++ 说这些是模棱两可的,
【发布时间】:2013-01-30 18:03:00
【问题描述】:

我必须重载移位运算符“

我在 ostream 重载方面做得很好,而我在重载 fstream 时遇到了一些问题,这里是:

在我的标题中:

friend ostream &operator<<(ostream &, const Fotografia &);
friend fstream &operator<<(fstream &, const Fotografia &);

在我的 cpp 文件中:

fstream &operator<<(fstream & miofile, const Fotografia & sorgente)
{
        //Open the file
        miofile.open("data.dat", ios::binary | ios::app);
        if(!miofile) cerr << "Can't open the file\n";
        miofile << strlen(sorgente.Titolo);
        miofile << endl;        
        miofile << sorgente.Titolo;
        //I close the file
        miofile.close();
        return miofile;

}

这是我面临的错误:

在函数`std::fstream& operator

ISO C++ says that these are ambiguous, even though the worst conversion for the first is    better than the worst conversion for the second:

std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>] 

std::fstream& operator<<(std::fstream&, const Fotografia&) 

到目前为止,我所理解的是,我刚刚创建的重载函数与标准 fstream

我认为我可以通过使用“::”范围调用 fstream 运算符来解决这个问题,但我不确定。

有人可以帮我吗? :)

编辑:

我正在发布标题的代码和构造函数的代码

         //Costruttore,distruttore,costruttore di copia,operatore di assegnazione.
         Fotografia(char * titolo = "Untitled" , char * formato = ".jpeg");
         ~Fotografia() { delete [] Titolo; delete [] Formato;}
         Fotografia(const Fotografia &);
         Fotografia &operator=(const Fotografia &);

这是在 cpp 中:

 Fotografia::Fotografia(char * titolo , char * formato)
 {
     Titolo = new char[strlen(titolo)+1];
     strcpy(Titolo,titolo);
     Formato = new char[strlen(formato)+1];
     strcpy(Formato,formato);
  } //Fine costruttore

【问题讨论】:

  • 一个fstream 是一个ostream
  • 传递给operator&lt;&lt;fstream不应该被调用者打开和关闭吗?因为它将它传入和传出函数是毫无意义的。关于您的问题:char*Fotografiaoperator char*Fotografia(const char*))之间是否有任何隐式转换?

标签: c++ ambiguous ambiguous-call


【解决方案1】:

去掉Fotografia中的operator char*,或者标记为explicit

此外,您可以插入任意basic_ostream,而不是将代码限制为插入fstream。这仍然适用于fstream,并且可以让您更灵活地使用其他形式的输出流。这也将消除错误。

【讨论】:

  • 我不确定我是否在关注你,Pete。 fstreamostream 最终都是 basic_ostream&lt;char&gt;。他可以尝试使用basic_ostream&lt;unsigned char&gt;,但我不确定他是否定义了行为。该标准没有为它指定任何语义,我怀疑像std::basic_ofstream&lt;unsigned char&gt; 这样的东西在大多数实现中都不起作用。 (就个人而言,我仍然认为制作 iostreams 模板,而不是仅仅提供两个单独的类,是一个错误。有点违反 KISS 原则,因为无论如何你只能使用两个实例化。)
  • @JamesKanze:charwchar_t 的不同类不会违反 DRY 吗?特别是在支持这两种情况的自定义流时。总是编写一个完全通用的模板并使用 sfinae 仅对流类启用它毕竟有点麻烦。
  • @JamesKanze - 嘿,我没有仔细阅读这个问题。尽管如此,我仍然支持结论,即使逻辑有缺陷。
  • @Grizzly 我不确定 DRY 是什么意思,但是......模板解决方案的问题之一是它强制两个类具有相同的语义。当他们真的不应该这样做时。
  • 我对我的第一篇文章进行了编辑,并为我的标题和构造函数添加了代码,我在任何地方都没有“operator char*”,你能指出我应该明确标记什么吗?非常感谢您的耐心和善意的帮助! ---编辑:明白了!谢谢你的帮助!!! ---
【解决方案2】:

std::fstream 重载operator&lt;&lt; 是没有意义的。 首先,因为有经验的 C++ 程序员几乎每次都使用 std::fstream;他们使用std::ofstreamstd::ifstream。 其次,因为一旦你使用了&lt;&lt;,返回值就是 std::ostream 无论如何,所以 operator&lt;&lt; 代表 ofstream 永远不会被调用。

当然,您对运算符的实现也违反了所有 的规则。您不会在运算符中打开或关闭文件; 运算符用于格式化数据。 如果你想 支持两种不同的格式,通常的方法是定义 一个操纵者在它们之间进行选择,并让客户决定。 (请参阅std::ios_base::xalloc() 和公司了解将 附加状态。)

【讨论】:

  • 好点,我会尝试相应地使用ofstream和ifstream而不是fstream。另外,我将让用户打开和关闭文件,同时操作员操作数据。感谢您的帮助!
猜你喜欢
  • 2018-06-01
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
相关资源
最近更新 更多