Solved

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

Posted on 2006-11-06
3
663 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

756 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