Solved

ostream overloading << must be declared with one parameter error in compile

Posted on 2006-11-06
3
659 Views
Last Modified: 2012-05-05
i get the following error in compile,
  ostream overloading << must be declared with one parameter error in compile.

this is the program - working on date formatting in c++

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class date
{

public:

      date(int day = 1, int month = 1, int year = 1940)
{

      this->day = day;
      this->month = month;
      this->year = year;
}

date(const long& date)
{
    year = date/10000;
    if (year < 100)
    {
           year = 1900 + year;
    }
     month = (date % 10000) / 100;
     day = date % 100;
}

void Setdate(date d)
{      
               day=d.GetDay();
                month=d.GetMonth();
              year=d.GetYear();
}      
int GetDay() const
{
     return day;
}

int GetMonth() const
{
     return month;
}

int GetYear() const
     {
return year;
}

date NextDate(date d)
      {
            date ndat;

            ndat=date((d.GetDay()+1),d.GetMonth(),d.GetYear());
            ndat=date(1,(d.GetMonth()+1),d.GetYear());
            ndat = date(1,1,(d.GetYear()+1));

return ndat;
      }

ostream& operator <<(ostream& os, const date& d)
{
      return os << d;
}

void tst_print()
{
      date d;
      cout << d;
}

private:
      int day;
      int month;
      int year;
      static int numOfDays[];
};

int main(void)
{

date d(8,11,2006);

d.tst_print();

return(0);
}
0
Comment
Question by:gbilios
  • 2
3 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17887623
First of all make it friend
friend ostream& operator <<(ostream& os, const date& d);
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 130 total points
ID: 17887631
>> return os << d;
This will be recursive.
Change this to something like

friend ostream& operator <<(ostream& os, const date& d) {
   return os << d.day << "/" << d.month << "/" << d.year << endl;
}

Or, whatever format you desire.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17887867
>>>> First of all make it friend

FYI:
the main issue for doing so is *not* to have access to private members (what is just an additional benefit) *but* to tell that the overload of operator<< is not a class member but a global function. Overloads of operator<<(ostream& os, ...)  must be made global cause the return value is ostream& and not the class you define. Non-global overloads of operator<< need to return a reference to the own class. Global overloads were needed so that you can mix different outputs, e. g.

   date d = "01/01/2007";
   string s = "Next new year is: ";

   cout << s << d << endl;

Any operand here uses a different overload of operator<<

Regards, Alex
 

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now