#include "yangrecord.h"
#ifndef yangllist_h
#define yangllist_h
class llist
{
private:
record *start;
char filename[16];
int readfile();
int writefile();
record * reverse(record *);
void cleanup();
public:
friend ostream& operator<<(llist&,ostream& os);
llist( );
llist(char []);
~llist( );
int addRecord(char[], char[], int, char[]);
int printRecord(char [ ]);
int modifyRecord(char[], char[], char[]);
void printAll();
int deleteRecord(char []);
void reverse();
};
#endif
#include"yangllist.h"
#include <iostream>
using namespace std;
int main()
{
char name[25] = "a";
char address[80] = "b";
int yearofbirth = 0;
char telno[15] = "c";
llist ls;
//ls.addRecord(name,name,yearofbirth,name);
ls.printAll();
system("pause");
}
ostream& operator<<(llist&,ostream& os)
{
int data = 0;
struct record *temp;
if (llist::start == NULL)
{
cout << "No Record Found\n" << endl;
}
else
{
temp = llist::start;
while (temp != NULL)
{
data++;
cout << "Record:" << data << endl;
cout << "Name:" << (temp->name) << endl;
cout << "Address:" << (temp->address) << endl;
cout << "Year of Birth:" << (temp->yearofbirth) << endl;
cout << "Telephone Number:" << (temp->telno) << endl;
temp = (temp->next);
}
}
return os;
}
C++ friend 问题,作业好烦,求助,多谢! 为什么我使用了friend,还是不能使用private的start?
答案:1 悬赏:0 手机版
解决时间 2021-03-16 02:55
- 提问者网友:寂寞梧桐
- 2021-03-15 21:02
最佳答案
- 五星知识达人网友:第四晚心情
- 2021-03-15 21:11
Note1:你的代码有其它的错误:
if (llist::start == NULL) 中,
start是nonstatic data member, 不能写 llist::start,
应该先写 ostream& operator<<(llist& L, ostream& os)
然後if改为
if (L.start == NULL)
Note2:
temp = llist::start;
也错了, 改法同Note2.
Note3:
private的start 可以用.
前两个错改好就可以通过编译.
Note4:
ostream& operator<<(llist& L,ostream& os)
写法很不正常. 这样对 llist的object k, 就不能写 cout << k; 变成是要写 k << cout;
应该是这样 ostream& operator<<(ostream& os, const llist& L)
if (llist::start == NULL) 中,
start是nonstatic data member, 不能写 llist::start,
应该先写 ostream& operator<<(llist& L, ostream& os)
然後if改为
if (L.start == NULL)
Note2:
temp = llist::start;
也错了, 改法同Note2.
Note3:
private的start 可以用.
前两个错改好就可以通过编译.
Note4:
ostream& operator<<(llist& L,ostream& os)
写法很不正常. 这样对 llist的object k, 就不能写 cout << k; 变成是要写 k << cout;
应该是这样 ostream& operator<<(ostream& os, const llist& L)
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯