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 ;
}