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

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?