普通网友 2014-03-04 07:53 采纳率: 100%
浏览 1855
已采纳

C++友元的问题。不知道错在哪里。弄得恼火了。。求助。

先不要管它干什么用,要怎样修改才可以运行呢?为什么要这样修改?

void set_input(){input=(input==TV)? DVD : TV;}
void settings() const;
void ch_h(Remote &t);
void input_ch_h(Remote &t);

private:
int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
};
class Remote
{
friend class Tv;
private:
int mode;
int style;

public:
enum{changgui,hudong};
Remote(int m=Tv::TV):mode(m){}
bool volup(Tv&t){return t.volup();}
bool voldown(Tv&t){return t.voldown();}
void onoff(Tv&t){t.onoff();}
void chanup(Tv&t){t.chanup();}
void chandown(Tv&t){t.chandown();}
void set_chan(Tv&t,int c){t.channel=c;}
void set_mode(Tv&t){t.set_mode();}
void set_input(Tv&t){t.set_input();}

};
inline void Tv::ch_h(Remote &t)
{
if(t.style==Remote::hudong)
t.style=Remote::changgui;
else t.style=Remote::hudong;
}
#endif

#include"151.h"
#include
bool Tv::volup()
{
if(volume {
volume++;
return true;
}
else
return false;
}
bool Tv::voldown()
{
if(volume>MinVal)
{
volume--;
return true;
}
else
return false;
}
void Tv::chanup()
{
if(channel channel++;
else
channel=1;
}
void Tv::chandown()
{
if(channel>1)
channel--;
else
channel=maxchannel;
}
void Tv::settings()const
{
std::cout<<"TV is "<<(state==Off? "Off":"On")<<std::endl;
if(state==On)
{
std::cout<<"Volume setting="<<volume<<std::endl;
std::cout<<"Channel setting="<<channel<<std::endl;
std::cout<<"Mode="<<(mode==Antenna? "antenna" : "cable")<<std::endl;
std::cout<<"Input="<<(input==TV? "TV":"DVD")<<std::endl;
}
}

#include
#include"151.h"
using namespace std;
bool Tv::volup()
{
if(volume {
volume++;
return true;
}
else
return false;
}
bool Tv::voldown()
{
if(volume>MinVal)
{
volume--;
return true;
}
else
return false;
}
void Tv::chanup()
{
if(channel channel++;
else
channel=1;
}
void Tv::chandown()
{
if(channel>1)
channel--;
else
channel=maxchannel;
}
void Tv::settings()const
{
cout<<"TV is "<<(state==Off? "Off":"On")<<endl;
if(state==On)
{
cout<<"Volume setting="<<volume<<endl;
cout<<"Channel setting="<<channel<<endl;
cout<<"Mode="<<(mode==Antenna? "antenna" : "cable")<<endl;
cout<<"Input="<<(input==TV? "TV":"DVD")<<endl;
}
}

  • 写回答

1条回答 默认 最新

  • fuding200209 2014-03-05 02:23
    关注

    代码贴的不完整,最好把完整代码贴出来,inline void Tv::ch_h(Remote &t),这个Tv类利用Remote类,这个你上前声明了Remote类么?

    inline void Tv::ch_h(Remote &t)
    {
    if(t.style==Remote::hudong)
    t.style=Remote::changgui;
    else t.style=Remote::hudong;
    }
    Tv类是Remote类的友元类,但Remote类并不是Tv类的友元类,总之代码不完整,不好看

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题