| 网站首页 | 文章中心 | 下载中心 | 编程世界论坛 | 图片中心 | 留言板 | 发布源码 | 
您现在的位置: 编程世界 >> 文章中心 >> VC文章 >> VC基础 >> 文章正文 用户登录 新用户注册
VC6冲破friend和cstring类的冲突,让friend和cstring类共存       ★★★ 【字体:
VC6冲破friend和cstring类的冲突,让friend和cstring类共存
作者:佚名    文章来源:本站原创    点击数:    更新时间:2007-12-19    

friend和cstring类的冲突问题大家应该都遇到过吧。

下面说一下出现这个现象的原因:

因为在Microsoft Visual C++ 6.0 中定义友元类需要的头文件是#include<iostream.h> 而在<iostream.h> 中没有重载 string类的 输出操作符 "<<";而<iostream>中重载了string类的输出操作符 "<<"。


1,请先看最初的代码及错误:

#include <iostream>
#include <string>
using namespace std;

class CA
{
public:
CA(){}
CA(string n,int a):name(n),age(a){}
friend ostream & operator << (ostream &,CA &);
private:
string name;
int age;
};

ostream & operator <<(ostream & output,CA & ca)
{
output<<"name:"<<ca.name<<endl;
output<<"age:"<<ca.age<<endl;
return output;
}

int main()
{
CA cc("luckydog",1);
cout<<cc<<endl;
return 0;
}

/*

--------------------Configuration: luckydog - Win32 Debug--------------------
Compiling...
luckydog.cpp
D:\C++\other\luckydog.cpp(18) : error C2248: 'name' : cannot access private member declared in class 'CA'
        D:\C++\other\luckydog.cpp(12) : see declaration of 'name'
D:\C++\other\luckydog.cpp(19) : error C2248: 'age' : cannot access private member declared in class 'CA'
        D:\C++\other\luckydog.cpp(13) : see declaration of 'age'
D:\C++\other\luckydog.cpp(26) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.

luckydog.obj - 1 error(s), 0 warning(s)

*/

备注:由于Microsoft Visual C++ 6.0中需要定义友元函数用friend 时需要用头文件 #include <iostream.h> 代替 #include <iostream> using namespace std; 所以出现上面所说的错误. 至于错误的原因我也不清楚,相关文章请点击该链接.

2.于是更改头文件,考虑到需要string类,所以需要加入名字空间std::string; 具体关于名字空间请查阅相关资料.更改后的程序以及错误如下:

#include <iostream.h>
#include <string>
using std::string;

class CA
{
public:
CA(){}
CA(string n,int a):name(n),age(a){}
friend ostream & operator << (ostream &,CA &);
private:
string name;
int age;
};

ostream & operator <<(ostream & output,CA & ca)
{
output<<"name:"<<ca.name<<endl;
output<<"age:"<<ca.age<<endl;
return output;
}

int main()
{
CA cc("luckydog",1);
cout<<cc<<endl;
return 0;
}

/*

--------------------Configuration: luckydog - Win32 Debug--------------------
Compiling...
luckydog.cpp
d:\c++\other\luckydog.cpp(18) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
执行 cl.exe 时出错.

luckydog.obj - 1 error(s), 0 warning(s)

*/

备注:错误原因:在#include<iostream.h>中string类没有重载"<<"操作符.

3.那么在Microsoft Visual C++ 6.0 中既要用到friend 又要用到string的输出的时候怎么办呢, 请看下面修改正确后的代码. 当然,要用using std::string; 那样#include<string> 和using std::string; 共同用来处理关于string类相关的操作.

#include <iostream.h>
#include <string>
using std::string;

class CA
{
public:
CA(){}
CA(string n,int a):name(n),age(a){}
friend ostream & operator << (ostream &,CA &);
private:
string name;
int age;
};

ostream & operator <<(ostream & output,CA & ca)
{
output<<"name:"<<ca.name.c_str()<<endl;             //注意这里的修改
output<<"age:"<<ca.age<<endl;
return output;
}

int main()
{
CA cc("luckydog",1);
cout<<cc<<endl;
return 0;
}

/*

--------------------Configuration: luckydog - Win32 Debug--------------------
Compiling...
luckydog.cpp

luckydog.obj - 0 error(s), 0 warning(s)
*/

备注:现在能正确运行了,请注意标记出来的那一部分的修改. .c_str().是用来把string类型的数据转化成char * .#include<iostream.h>中"<<"能对char * 正确输出。因此能正确运行。

题外:关于此类的问题,我只找到这一种解决方案。如果有更好的解决方案,希望分享。另外如果此篇文章有任何不对的地方欢迎大家指正,因为我也是VC初学者了。

文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    VC6做简易自动升级程序
    防止VC的edit控件密码被非法
    VC播放资源文件中的声音
    一个自写的显示GIF和JPG的类
    VC实现删除文件到回收站
    VC多线程之线程间的通信
    VC另关闭按钮(X按钮)变灰失效
    vc全局钩子实现程序监控
    创建只能建一个对象的类
    读写注册表API,适用于VC也适
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)