Error trying to make operator overload with dates [closed]

-3

I'm trying to overload operators for dates, but if I use a double overhead it gives error.

What I actually want to do is that if the user uses a separator setting a format it accepts and if the user does not use it get the default format defined.

#include <iostream>

class Date   {  
    int dia, mes, ano;
    std::string sep; public:  

    Date(int _d, int _m, int _a, std::string _sep): dia(_d), mes(_m), ano(_a), sep(_sep){}

    Date(int _d, int _m, int _a): dia(_d), mes(_m), ano(_a){}  

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

std::ostream& operator<<(std::ostream& os, const Date& sepdate)   {  
    oss << sepdate.dia << sepdate.sep << sepdate.mes << sepdate.sep << sepdate.ano << "\n\n";  
    return oss;   }  

std::ostream& operator<<(std::ostream& os, const Date& date)   {
    os << date.dia << "/" << date.mes << "/" << date.ano << "\n\n";
    return os;   }


int main()   {  
    Date sepdate(01, 01, 2017, "-");
    std::cout << "\n\tThe date in sep " << sepdate << std::flush;

    Date date(07,07,2017);
    std::cout << "\n\tThe date is " << date << std::flush;    }

/* Saida:    bash-4.3$ ./operator

        The date in sep 1-1-2017


        The date is 772017
*/

Ele teria que retornar a seguinte saida:
/* Saida:    bash-4.3$ ./operator

        The date in sep 1-1-2017


        The date is 7/7/2017
*/
    
asked by anonymous 07.07.2017 / 23:56

3 answers

1

First, the second constructor is not setting the separator (the string sep ) with a default value you set. The assigned tab, therefore, is "" .

Date(int _d, int _m, int _a, std::string _sep): dia(_d), mes(_m), ano(_a), sep(_sep){}
Date(int _d, int _m, int _a): dia(_d), mes(_m), ano(_a){} 

I think you prefer to set the default value "/" , so you do not need to create any additional method (neither the constructor nor the operator) that is just to handle the case of not setting the tab. It may look like this:

Date(int _d, int _m, int _a, std::string _sep="/"): dia(_d), mes(_m), ano(_a), sep(_sep){}

.

Second, shortly after declaring the first operador<< without body, you closed bracket without opening, it is syntax error. I do not know how you could compile like this ...

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

.

Third, the first operator setting has a typing error. The parameter is os but the code uses oss , which was not declared as a parameter. It is a semantic error. I also do not know how you compiled this error ...

std::ostream& operator<<(std::ostream& os, const Date& sepdate)   {  
    oss << sepdate.dia << sepdate.sep << sepdate.mes << sepdate.sep << sepdate.ano << "\n\n";  
    return oss;   }   

.

Fourth, the two definitions of operators have the same signature:

ostream& operator<<( ostream& , const Date& ) ;

and this makes the operator ambiguous even for those who would invoke friend . I do not want to separate methods of the same signature between friend and non friend , for me this is semantic error, but if compiled so I can be wrong ...

.

Finally, to finish I recommend not only correct this but also adopt better programming practices to make your code more readable. These two definitions of methods, for example, are very illegible.

Date(int _d, int _m, int _a, std::string _sep): dia(_d), mes(_m), ano(_a), sep(_sep){} 

std::ostream& operator<<(std::ostream& os, const Date& sepdate)   {  
    oss << sepdate.dia << sepdate.sep << sepdate.mes << sepdate.sep << sepdate.ano << "\n\n";  
    return oss;   }  

Avoid leaving lines of code too long, use alternatives that reduce their size (even with more lines). Also use spaces to better separate the elements to make it easier to read them. It's also good to use using namespace std ; to reduce code. Indent the code correctly and avoid keys in the same line as the codes they involve. If you do this, the parts I've just shown can look like this, for example.

Date( int _d , int _m , int _a , string _sep ):
dia( _d ) ,
mes( _m ) ,
ano( _a ) ,
sep( _sep ) {
    // Sem código.
} 

ostream& operator<<( ostream& os , const Date& sepdate ) {  
    os << sepdate.dia << sepdate.sep << sepdate.mes ;
    os << sepdate.sep << sepdate.ano << "\n\n" ;
    return os ;
}  

It got a lot better, do not you think? If it's not for that, it may just be that way.

Date( int _d , int _m , int _a , std::string _sep ):
dia(_d) , mes(_m) , ano(_a) , sep(_sep) { } 

std::ostream& operator<<( std::ostream& os , const Date& sepdate ) {  
    os << sepdate.dia << sepdate.sep << sepdate.mes ;
    os << sepdate.sep << sepdate.ano << "\n\n" ;
    return os ;
}  

Anyway, my code would look like this.

# include <iostream>

class Date {  
   int dia , mes , ano ;
   std::string sep ;

   public:  

      Date( int _d , int _m , int _a , std::string _sep="/" ):
         dia(_d) , mes(_m) , ano(_a) , sep(_sep) { }

      friend std::ostream& operator<<( std::ostream& os , const Date& date ) {
         os << date.dia << date.sep << date.mes ;
         os << date.sep << date.ano << "\n\n" ;
         return os ;
      }
} ;

int main( int ac , char **av ) {  
   Date sepdate( 1 , 1 , 2017 , "-" ) ;
   std::cout << "\n\tThe date is sep " ;
   std::cout << sepdate << std::flush ;
   Date date( 7 , 7 , 2017 ) ;
   std::cout << "\n\tThe date is " ;
   std::cout << date << std::flush ;
}
    
12.07.2017 / 06:12
1

The program shown or compiled, because there is a duplicate definition of the output operator. Also the formatting is awful, not a typical formatting used with C ++.

Obs. Numeric literals beginning with '0' indicate octal numbers. It should not be used to indicate decimal numbers, such as '01' and '07' above.

Below is an improved version, which compiles and shows the date bars on output.

#include <iostream>

class Date
{  
   int dia, mes, ano;
   std::string sep;

   public:  
      Date(int _d, int _m, int _a, std::string _sep): dia(_d), mes(_m), ano(_a), sep(_sep) { }

      Date(int _d, int _m, int _a): dia(_d), mes(_m), ano(_a) { }

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

// std::ostream& operator<<(std::ostream& os, const Date& sepdate)
// {
//    os << sepdate.dia << sepdate.sep << sepdate.mes << sepdate.sep << sepdate.ano << "\n\n";  
//    return os;
// }

std::ostream& operator<<(std::ostream& os, const Date& date)
{
   os << date.dia << "/" << date.mes << "/" << date.ano << "\n\n";
   return os;
}

int main()
{  
   Date sepdate(1, 1, 2017, "-");
   std::cout << "\n\tThe date is sep " << sepdate << std::flush;

   Date date(7,7,2017);
   std::cout << "\n\tThe date is " << date << std::flush;
}
    
08.07.2017 / 16:13
0

If I understand your question correctly, you want the "/" to appear in each element on the date.

To do this, you must format your text:

std::put_time(&date, "%d/%m/%Y");
    
08.07.2017 / 00:24