为了账号安全,请及时绑定邮箱和手机立即绑定

构造函数出错

#include <iostream>

using namespace std;

class Watch;


/**

 * 定义Time类

 * 数据成员:m_iHour, m_iMinute,m_iSecond 

 * 成员函数:构造函数

 * 友元类:Watch

 */

class Time

{

    // 友元类

    friend Watch;

public:

Time(int hour, int min, int sec)

{

m_iHour = hour;

m_iMinute = min;

m_iSecond = sec;

}

public:

int m_iHour;

int m_iMinute;

int m_iSecond;

};


/**

 * 定义Watch类

 * 数据成员:m_tTime

 * 成员函数:构造函数

 * display用于显示时间

 */

class Watch

{

public:

Watch(Time t):m_tTimer(t.m_iHour,t.m_iMinute,t.m_iSecond)

{}

void display()

{

cout << m_tTime.m_iHour << endl;

cout << m_tTime.m_iMinute << endl;

cout << m_tTime.m_iSecond << endl;

}

public:

Time m_tTime;

};


int main()

{

Time t(6, 30, 20);

Watch w(t);

w.display();


return 0;

}

这是哪里出了问题?

正在回答

7 回答

Watch(Time t):m_tTimer(t.m_iHour,t.m_iMinute,t.m_iSecond);

这里你写成m_tTimer当然不对啦,应该是m_tTime才对啊

1 回复 有任何疑惑可以回复我~

Watch(Time t):m_tTimer(t.m_iHour,t.m_iMinute,t.m_iSecond);//m_tTime

1 回复 有任何疑惑可以回复我~

不是构造函数错了,构造函数没有错


0 回复 有任何疑惑可以回复我~

应该送 friend class Watch你写成friend Watch

0 回复 有任何疑惑可以回复我~

m_tTimer有问题

0 回复 有任何疑惑可以回复我~

不对,不改也能运行,没有报错


0 回复 有任何疑惑可以回复我~
#1

慕设计4173655

没有 & 是用两个对象赋值,有 & 是作引用,减小内存的作用。
2018-11-22 回复 有任何疑惑可以回复我~

Watch的构造函数

Watch(Time &t)

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之模板篇
  • 参与学习       91174    人
  • 解答问题       318    个

本C++教程力求即学即会,所有知识以实践方式讲解到操作层面

进入课程

构造函数出错

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信