啊!!我要暴粗啊
compiler: vc++ 6.0
define friend funtion:freind ostream & operator<< (ostream & os, const Student & t)
- C/C++ code
ostream & operator<< (ostream & os, const Student & t){ os << t.m_id; os << t.m_name; os << t.m_sex; return os; if (t.m_time == NULL) { return os; }}//:\c or c++ project\randomcall\student.cpp(40) : error C2248: 'm_id' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(14) : see declaration of 'm_id'
d:\c or c++ project\randomcall\student.cpp(41) : error C2248: 'm_name' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(15) : see declaration of 'm_name'
d:\c or c++ project\randomcall\student.cpp(42) : error C2248: 'm_sex' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(16) : see declaration of 'm_sex'
d:\c or c++ project\randomcall\student.cpp(46) : error C2248: 'm_time' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(17) : see declaration of 'm_time'
d:\c or c++ project\randomcall\student.cpp(55) : error C2248: 'm_time' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(17) : see declaration of 'm_time'
d:\c or c++ project\randomcall\student.cpp(57) : error C2248: 'm_time' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(17) : see declaration of 'm_time'
d:\c or c++ project\randomcall\student.cpp(59) : error C2248: 'm_time' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(17) : see declaration of 'm_time'
d:\c or c++ project\randomcall\student.cpp(61) : error C2248: 'm_time' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(17) : see declaration of 'm_time'
d:\c or c++ project\randomcall\student.cpp(63) : error C2248: 'm_time' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(17) : see declaration of 'm_time'
d:\c or c++ project\randomcall\student.cpp(65) : error C2248: 'm_time' : cannot access private member declared in class 'Student'
d:\c or c++ project\randomcall\student.h(17) : see declaration of 'm_time'
[解决办法]
你确定你在Student里面加了 friend function声明?
[解决办法]
要把 ostream & operator<< 在student类中置为友元
[解决办法]
你有在类Student里面申明freind ostream & operator<< (ostream & os, const Student & t)吗??
if (t.m_time == NULL)
{
return os;
}
上面这个程序段有什么用呢?你在这上面就使用return返回了啊??
[解决办法]
学生嘛就要开放点 public
[解决办法]
友元定义没弄错吧?会不会是那个上世纪的古董有bug?
哪啥,你代码后面的if语句哪里能执行呢?
[解决办法]
[解决办法]
[解决办法]
9楼10楼又没了。改版后的CSDN好像特喜欢丢楼。
[解决办法]
那你试试看换成VS2010,闹不好就直接pass了。
[解决办法]
这是vc6的bug,友元定义必须放进类体中!
vc6与标准相差 太大了,还抱着它干吗?