Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9208389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:36:27+00:00 2026-06-18T00:36:27+00:00

I have to overload the shift operator << both for writing in console and

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:36:28+00:00Added an answer on June 18, 2026 at 12:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Overload ++ operator Say i have a class in which i overloads
I have a Card class and I want to overload the > operator to
I want to overload a cast operator, I have the following piece of code
I have typedef std::string OrderID; I would like to overload the operator ++ for
I'm trying to overload the '--' postfix operator. I have this code: class Counter
Why would you need to overload the [] operator? I have never come across
Possible Duplicate: Operator[][] overload I have made class which contains an array containing (in
I have a smart pointer class and I want to overload operator-> ; it's
i have a problem about conversion of type using overload of operator, here i
I overloaded the << operator of a class. How do I have to overload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.