普通网友 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类的友元类,总之代码不完整,不好看

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

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题