返回   cpper编程论坛 > Zion/测试
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

Zion/测试 惹人烦的东西这边来

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2005-07-12
普通会员
 
注册日期: 2003-11-14
帖子: 31
wraith 正向着好的方向发展
默认 关于重载<<的问题。

代码:
class Symptom { friend ostream& operator<< ( ostream&, const Symptom&); friend void test2(const Symptom&); friend void test(const Symptom& ); private: string m_sSymptom; string m_sDescription; } void test(const Symptom& sym) { sym.m_sSymptom.size(); } void test2(const Symptom& sym) { sym.m_sSymptom; } ostream& operator<< ( ostream& os, const Symptom& sym ) { os << sym.m_sSymptom; return os; }
test,test2都通过了调试,只有<<中告诉我不允许使用private member.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2005-07-12
Elminster 的头像
超级版主
 
注册日期: 2002-09-09
帖子: 1,764
Elminster 正向着好的方向发展
默认

你有没有从上到下仔细看过你的编译器给出的错误提示?把你的环境和相关信息提交完整,这样才好方便别人帮你发现你的问题。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2005-07-12
普通会员
 
注册日期: 2003-11-14
帖子: 31
wraith 正向着好的方向发展
默认

使用的是win32 application. 这个cpp用的是automatic use precompiled header 选项。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2005-07-12
普通会员
 
注册日期: 2003-11-14
帖子: 31
wraith 正向着好的方向发展
默认

代码:
#ifndef SYMPTOM_H #define SYMPTOM_H #include "stdafx.h" #include <iostream> using namespace std; class Symptom { friend ostream& operator<< ( ostream& os, const Symptom& sym ) { os << sym.m_sSymptom; return os; } friend void test2(const Symptom&); friend void test(const Symptom& ); private: string m_sSymptom; string m_sDescription; } endif
很奇怪,这样就能通过编译。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #5 (permalink)  
旧 2005-07-12
Elminster 的头像
超级版主
 
注册日期: 2002-09-09
帖子: 1,764
Elminster 正向着好的方向发展
默认

引用:
作者: wraith
使用的是win32 application. 这个cpp用的是automatic use precompiled header 选项。
看来以后应该学习 javaeye ,给提问需要提供的信息列个清单:

第一、你至少应该告诉别人,你用的是什么编译器,什么版本,这个世界上并不是只有 VC++ 一种编译器。
第二、你至少应该告诉别人,你的程序是编译时候出错,还是连接时候出错,然后给出详细的错误信息。
第三、你应该给出完整的源代码,如果不方便这么做,那么应该说明是不是节选摘要,以及其他相关的内容如何。比方说你上面这个程序,是完整程序吗?还是只是摘出来贴了一段?是从编译器中直接拷贝出来的吗?还是另外自己重新手打的?

这些问题不明确,别人就得浪费力气去猜。虽然有时侯能够猜出结果 ── 比如说你贴出来的程序里面,在一个类定义结束的 } 后面都忘了分号,我估计这多半就是原因 ── 但是那样是浪费别人和你自己的时间与精力,所以这样的问法是不受欢迎的。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #6 (permalink)  
旧 2005-07-12
普通会员
 
注册日期: 2003-11-14
帖子: 31
wraith 正向着好的方向发展
默认

对不起,我提问确实没有考虑这么周到。
因为是节选的程序,所以自己在贴得时候忘了加分号,以后一定注意。
1。我用的是vc++ 6.0
2.我只是编译这一个sym.cpp和sym.h时出的错,没有链接。
3.是贴了一部分,自己加的}所以忘了;.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #7 (permalink)  
旧 2005-07-13
zweily 的头像
版主
 
注册日期: 2002-09-24
帖子: 425
文章: 10
zweily 正向着好的方向发展
默认

楼主把你这个函数在类的定义之前声明一下吧。
就像下面这样:
代码:
#ifndef SYMPTOM_H #define SYMPTOM_H #include "stdafx.h" #include <iostream> using namespace std; class Symptom; ostream& operator<< ( ostream& os, const Symptom& sym ); class Symptom { friend ostream& operator<< ( ostream& os, const Symptom& sym ); friend void test2(const Symptom&); friend void test(const Symptom& ); private: string m_sSymptom; string m_sDescription; }; #endif
之后再定义你的<<运算符。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #8 (permalink)  
旧 2005-07-13
普通会员
 
注册日期: 2003-11-14
帖子: 31
wraith 正向着好的方向发展
默认

多谢zweily.果然通过了,为什么会这样呢?我翻了很多资料都没有过这样的用法。请指点一下。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #9 (permalink)  
旧 2005-07-13
zweily 的头像
版主
 
注册日期: 2002-09-24
帖子: 425
文章: 10
zweily 正向着好的方向发展
默认

因为friend声明并没有将其名字引入作用域,而你include了iostream,并且using了std,所以iostream中的<<暴露出来了。

如果你去掉那个using namespace std; 然后注意在你类里面或者其他函数定义里面是用到std中的东西的时候加上std::,这样应该也能解决你的问题。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #10 (permalink)  
旧 2005-07-14
普通会员
 
注册日期: 2003-11-14
帖子: 31
wraith 正向着好的方向发展
默认

多谢。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #11 (permalink)  
旧 2005-07-20
高级会员
 
注册日期: 2003-11-02
帖子: 682
andy84920 正向着好的方向发展
发送 MSN 消息给 andy84920
默认

引用:
作者: zweily
因为friend声明并没有将其名字引入作用域,而你include了iostream,并且using了std,所以iostream中的<<暴露出来了。

如果你去掉那个using namespace std; 然后注意在你类里面或者其他函数定义里面是用到std中的东西的时候加上std::,这样应该也能解决你的问题。
friend声明并没有将其名字引用作用域?(有标准的规定要先在外声明吗?)我用DEV-C++编译没有问题.因为没有VC,楼主说的错误是"只有<<中告诉我不允许使用private member"如果是iostream中的<<暴露出来这个和编译错误结果相符吗?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签
主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 02:09 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0