First, the second constructor is not setting the separator (the string sep
) with a default value defined by you. The assigned separator is therefore ""
.
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 it is not necessary to create any additional method (neither the constructor nor that operator) which is only to deal with the case of non-definition of the separator. You can stay like this:
Date(int _d, int _m, int _a, std::string _sep="/"): dia(_d), mes(_m), ano(_a), sep(_sep){}
.
Second, just after declaring the first operador<<
without body, you closed bracket without opening, it is syntax error. I do not know how you could compile so...
friend std::ostream& operator<<(std::ostream& os, const Date& date); };
.
Third, the first definition of the operator has typing error. The parameter is os
but the code uses oss
, which was not declared as parameter. It is semantic error. I also don’t know how you compiled with 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 that makes the operator ambiguous even to whom would invoke the friend
. I don’t think we can separate methods of the same signature between friend
and not friend
, for me this is semantic error, but if compiled so I can be wrong...
.
Anyway, to finish I recommend not only fix 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 elements to make them easier to read. It’s also good to use the using namespace std ;
to reduce the code. Identify the code correctly and avoid keys in the same row of codes that they involve. If I do all this, the parts I just showed might 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’s gotten a lot better, don’t you think? If it’s not for that, it might just be like this.
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 ;
}
Do pq not change the constructor and use only one output operator ? ... Date(int _d, int _m, int _a): dia(_d), mes(_m), ano(_a), Sep("/") {}
– Felipe