首页 > 编程语言 > 详细

c++构造函数重载问题

时间:2021-01-06 23:47:19      阅读:3      评论:0      收藏:0      [点我收藏+]

 

 

#include <iostream>                                                              
 
using namespace std;            
 
class Time;                     
class Time                      
{
    friend ostream& operator << (ostream &os, const Time &t);
    friend Time operator + (const Time & t1, const Time & t2);
    private:                    
        int hours;              
        int minutes;            
    public:                     
        Time();                 
        Time(int h, int m = 0 ) 
        {                       
            hours = h;          
            minutes = m;        
        }                       
        void AddMin(int m);     
        void AddHr(int h);      
        void Reset(int h=0, int m=0);              
        Time Sum(const Time &t) const;             
//      Time operator + (const Time &t) const;     
        void Show() const;      
 
};                              
 
Time::Time()                    
{
    hours = 0;                  
    minutes = 0;                
}
                              
void Time::AddMin(int m)        
{
    minutes += m;               
    hours = minutes / 60;       
    minutes %= 60;              
}
 
void Time::AddHr(int h)         
{
    hours += h;                 
 
}
 
Time Time::Sum(const Time &t) const                
{
    Time sum;                   
    sum.minutes = minutes + t.minutes;             
    sum.hours = hours + t.hours + sum.minutes / 60;
    sum.minutes %= 60;          
    return sum;                 
 
 
}
 
 
Time operator + (const Time & t1, const Time & t2)
{
    Time sum;                   
    sum.minutes = t1.minutes + t2.minutes;
    sum.hours = t1.hours + t2.hours + sum.minutes / 60;
    sum.minutes %= 60;          
    return sum;                 
}
ostream& operator << (ostream &os, const Time &t)
{
    os << t.hours << ":" << t.minutes;
    return os;
}


int main()
{
    Time a;

    return 0;
}  

 

 


 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Time;
 6 class Time
 7 {
 8     friend ostream& operator << (ostream &os, const Time &t);
 9     friend Time operator + (const Time & t1, const Time & t2);
10     private:                                                                     
11         int hours;
12         int minutes;
13     public:
14         Time();
15         Time(int h, int m = 0 ); 
16         void AddMin(int m);
17         void AddHr(int h);
18         void Reset(int h=0, int m=0);
19         Time Sum(const Time &t) const;
20 //      Time operator + (const Time &t) const;
21         void Show() const;
22 
23 };
24 
25 Time::Time()
26 {
27     hours = 0; 
28     minutes = 0; 
29 }
30 
31 Time::Time(int h, int m = 0) 
32 {
33     hours = h; 
34     minutes = m; 
35 }
36 
37 void Time::AddMin(int m) 
38 {
39     minutes += m; 
40     hours = minutes / 60;
41     minutes %= 60;
42 }
43 
44 void Time::AddHr(int h) 
45 {
46     hours += h; 
47 
48 }
49 
50 Time Time::Sum(const Time &t) const
51 {
52     Time sum;
53     sum.minutes = minutes + t.minutes;
54     sum.hours = hours + t.hours + sum.minutes / 60;
55     sum.minutes %= 60;
56     return sum;
57 
58 
59 }
60 
61 
62 Time operator + (const Time & t1, const Time & t2)
63 {
64     Time sum;
65     sum.minutes = t1.minutes + t2.minutes;
66     sum.hours = t1.hours + t2.hours + sum.minutes / 60;
67     sum.minutes %= 60;
68     return sum;
69 }
70 
71 ostream& operator << (ostream &os, const Time &t)
72 {
73     os << t.hours << ":" << t.minutes;
74     return os;
75 }
76 
77 
78 int main()
79 {
80     Time a;
81 
82     return 0;
83 }  

 

出错  意思是声明或者定义只需要出现一次默认参数的值,将15行或者31行中的默认参数删除一个即可

[email protected]:~/cpp_tset/cpp_develop$ g++ operator_test1.cpp -o operator_test1    
operator_test1.cpp:31:28: error: default argument given for parameter 2 of ‘Time::Time(int, int)’ [-fpermissive]
 Time::Time(int h, int m = 0)
                            ^
operator_test1.cpp:15:3: note: previous specification in ‘Time::Time(int, int)’ here                                                                              
   Time(int h, int m = 0 );

 

c++构造函数重载问题

原文:https://www.cnblogs.com/miaorn/p/14243916.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号