ISO C++说这些是模糊的,

aro*_*man 5 c++ ambiguous ambiguous-call

我必须重载移位运算符"<<",以便在控制台中写入和在二进制文件上写入.

我正在为ostream重载做好,而我在重载fstream时遇到一些问题,这里是:

在我的标题中:

friend ostream &operator<<(ostream &, const Fotografia &);
friend fstream &operator<<(fstream &, const Fotografia &);
Run Code Online (Sandbox Code Playgroud)

在我的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;

}
Run Code Online (Sandbox Code Playgroud)

这是我面临的错误:

在函数`std :: fstream&operator <<(std :: fstream&,const Fotografia&)'中:

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&) 
Run Code Online (Sandbox Code Playgroud)

到目前为止我所理解的是,我刚刚创建的重载函数与标准的fstream <<之间存在模糊性.现在,我不明白为什么,因为我的重载函数应该只适用于"Fotografia"类(由我创建),而我正在尝试编写一个char*.

我以为我可以通过调用带有"::"范围的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 &);
Run Code Online (Sandbox Code Playgroud)

这是在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
Run Code Online (Sandbox Code Playgroud)

Pet*_*ker 4

去掉operator char*in Fotografia,或者标记它explicit

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