0

I have a Date class which I am trying to test with a simple program to ask the user to input a date in a certain format which will then be put into the class. The idea is to allow the user to set a Date class from any string of text starting with the correct format.

Here is my date class header file (the implimentations of the other functions aren't relevant to this post):

#ifndef DATE_HPP_
#define DATE_HPP_

#include <iostream>
#include <cstdio>

class Date {
public:
int Year;
int Month;
int Day;
int HH;
int MM;
int ss;
Date();

int getTotalSeconds();

/*
 * Overloaded Operator Functions
 */
//Assignments
Date operator=(Date input);
//Comparisons
bool operator==(Date& rhs);
bool operator!=(Date& rhs);
bool operator<(Date& rhs);
bool operator>(Date& rhs);
bool operator<=(Date& rhs);
bool operator>=(Date& rhs);
//Conversion
operator char*();
operator std::string();

//Declared as member functions
std::ostream& operator<<(std::ostream& os){
    os << "operator<<: " << this->Year << '-' << this->Month << '-' << this->Day << '-' << this->HH << ':' << this->MM << ':' << this->ss;
    return os;
}

std::istream& operator>>(std::istream& is){
    char input[20];
    is >> input;
    scanf(input,"%04d-%02d-%02d-%02d:%02d:%02d",Year,Month,Day,HH,MM,ss);
    is.clear();
    return is;
}

};

#endif

My test program looks like this:

#include <iostream>
#include "Date.hpp"

int main(int argc, char* argv[]){
    Date date;
    std::cout << "Date initialized, printing: \n" << date << std::endl;

    std::cout << "This is a test of the date library!\nPlease enter a date in the form YYYY-MM-DD-HH:mm:ss: ";
    std::cin >> date;
    std::cout << "\n\nDate reset, printing:\n" << date << std::endl << "Exit!\n";
    return 0;
}

I don't know exactly what I am doing wrong. I've been looking up information on overloading operators and the operator<< works great! (I compiled and tested everything before I tried overloading operator>>) If it helps, I am using gcc on arch linux.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
KG6ZVP
  • 3,610
  • 4
  • 26
  • 45

1 Answers1

2

Your operator << and operator >> functions take one parameter when they should actually be taking two. They should also be friends of the class This:

std::ostream& operator<< (std::ostream& os)

should be

friend std::ostream& operator<< (std::ostream& os, Date const& date)

and inside the body of the function you use date.[member] to access the data members. Same goes for the operator >> function, only the parameter it takes should be a non-const reference.

David G
  • 94,763
  • 41
  • 167
  • 253
  • These should then be moved outside of the class. The OP had them inside. – pippin1289 Oct 10 '13 at 20:19
  • I did as you suggested and declared it as friend and it compiles now. However, the output of my test program is this: $ ./date_test Date initialized, printing: 0000-00-00-00:00:00 This is a test of the date library! Please enter a date in the form YYYY-MM-DD-HH:mm:ss: 2013-04-08-13:26:34 Date reset, printing: 0000-00-00-00:00:00 Exit! – KG6ZVP Oct 10 '13 at 20:27
  • @KG6ZVP You must have done something wrong, but without seeing your latest code it's hard to guess what that might be. – john Oct 10 '13 at 20:29
  • The code stayed the same, with these exceptions: It is now declared so: friend std::istream& operator>>(std::istream& is, Date& date); and all variables are accessed so: date.Year – KG6ZVP Oct 10 '13 at 20:45
  • @KG6ZVP Use `sscanf` rather than `scanf`. – David G Oct 10 '13 at 20:49
  • I changed scanf( to sscanf( and I got this: Please enter a date in the form YYYY-MM-DD-HH:mm:ss: 2013-04-08-15:55:06 Segmentation fault (core dumped) – KG6ZVP Oct 10 '13 at 21:22
  • @KG6ZVP: of course, it crashed! For `sscanf()` (and `scanf()` for that matter) you need to pass pointers - not values - matching the format string. – Dietmar Kühl Oct 10 '13 at 21:53