Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ISO C++ says that these are ambiguous,

I have to overload the shift operator " << " both for writing in console and to write on a binary file..

I am doing okay for the ostream overloading, while I am having some problem overloading the fstream, here it is:

in my header:

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

in my cpp file:

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;

}

Here's the error I am facing:

In function `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&) 

What I understood so far is that there's ambiguosity between the overloaded function I just created and the standard fstream << . Now, what I do not understand is why, because my overloaded function should work just for the class "Fotografia" (which was created by me), while I am trying to write a char * .

I thought I could solve this problem by calling the fstream operator with the "::" scope but I am not sure.

Could anyone help me out here please? :)

EDIT:

I am posting the code for the header and the code for the constructor

         //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 &);

This is in the 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
like image 562
arocketman Avatar asked Jan 30 '13 18:01

arocketman


2 Answers

Get rid of the operator char* in Fotografia, or mark it explicit.

Also, instead of limiting the code to inserting in an fstream, you could insert into an arbitrary basic_ostream. That would still work for an fstream, and would give you more flexibility to use other forms of output streams. That would also eliminate the error.

like image 95
Pete Becker Avatar answered Oct 26 '22 01:10

Pete Becker


It makes no sense to overload operator<< for std::fstream. First, because experienced C++ programmers hardly every use std::fstream; they use std::ofstream or std::ifstream. And second, because once you've used a <<, the return value is a std::ostream anyway, so the operator<< for ofstream would never be called.

Of course, your implementation of the operator also violates all of the rules. You don't open or close a file in the operator; the operator is for formatting the data. If you want to support two different formats, the usual way would be to define a manipulator to choose between them, and let the client decide. (See std::ios_base::xalloc() and company for where to put the additional state.)

like image 26
James Kanze Avatar answered Oct 26 '22 03:10

James Kanze