关于<<重载的问题...

kesaihao862 2008-04-08 11:19:14
#include <iostream>
using namespace std;
class WordCount {
friend ostream&
operator<<(ostream&, const WordCount&);
public:
WordCount( string word, int cnt=1 );
// ...
private:
string word;
int occurs;
};
ostream&
operator <<( ostream& os, const WordCount& wd )
{ // 格式: <occurs> word
os << "< " << wd.occurs << " > "<< wd.word;
return os;
}


int main()
{
WordCount wd( "sadness", 12 );
cout << "wd:\n" << wd << endl;
return 0;
}


我这个在VC6下重载<<操作符的程序为什么会通过过呢,明明声明的是友元啊..谁能帮我改下,可以在VC6下运行,等着急用...谢谢
...全文
170 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2008-04-08
  • 打赏
  • 举报
回复
class WordCount {
friend ostream&
operator<<(ostream&, const WordCount&)
{ // 格式: <occurs> word
os << "< " << wd.occurs << " > "<< wd.word;
return os;
}


public:
WordCount( string word, int cnt=1 );
// ...
private:
string word;
int occurs;
};
ttkk_2007 2008-04-08
  • 打赏
  • 举报
回复
扔了vc6
ryfdizuo 2008-04-08
  • 打赏
  • 举报
回复
只是一个警告吧,有可能会堆栈溢出...
星羽 2008-04-08
  • 打赏
  • 举报
回复


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

class WordCount {
friend ostream& operator<<(ostream&, const WordCount&);
public:
WordCount( string word, int cnt=1 );
private:
string word;
int occurs;
};

WordCount::WordCount(string word, int cnt/* =1 */)
{
this->word = word;
occurs = cnt;
}

ostream& operator <<( ostream& os, const WordCount& wd )
{
os << "< " << wd.occurs << " > "<< wd.word;
return os;
}


int main()
{
WordCount wd( "sadness", 12 );
cout << "wd:\n" << wd << endl;
return 0;
}

baihacker 2008-04-08
  • 打赏
  • 举报
回复
VC6的有问题..
#include <string>
并实现WordCount的constructor后DEVC++通过
wuyecandi 2008-04-08
  • 打赏
  • 举报
回复
没定义构造函数,
IT_lau 2008-04-08
  • 打赏
  • 举报
回复
up
cx36001390 2008-04-08
  • 打赏
  • 举报
回复
1.vc6环境下这句话不要忘记加.
#include "stdafx.h"

2.构造函数的参数不要和成员变量同名.
Vitin 2008-04-08
  • 打赏
  • 举报
回复
构造函数的问题不谈。单就友元而言,我知道VC6开放std名字空间后operator<<友元会有问题的。

可以去掉 using namespace std; 试试,如
#include <iostream>
#include <string>
using std::ostream;
using std::string;
using std::cout;
using std::endl;
...
就呆在云上 2008-04-08
  • 打赏
  • 举报
回复
你的构造函数有问题
这个东西不是vc6的问题吧,不过我挺讨厌vc6的
修改如下:
#include <iostream>
#include <string>
using namespace std;
class WordCount {
friend ostream&
operator<<(ostream&, const WordCount&);
public:

WordCount( string worddd , int cnt=1 );
// ...
private:
string word;
int occurs;
};


ostream&
operator <<( ostream& os, const WordCount& wd )
{ // ¸ñʽ: <occurs> word
os << "< " << wd.occurs << " > "<< wd.word;
return os;
}
WordCount::WordCount( string worddd, int cnt)
{
word = worddd;
occurs = cnt;
}

int main()
{
WordCount wd( "sadness", 12 );
cout << "wd:\n" << wd << endl;
return 0;
}
wuyn_1984 2008-04-08
  • 打赏
  • 举报
回复
7楼是亮点
hastings 2008-04-08
  • 打赏
  • 举报
回复
试试:
#include <iostream>
#include <string>
using namespace std;
class WordCount ;
ostream&
operator <<( ostream& os, const WordCount& wd );
class WordCount {
friend ostream&
operator<<(ostream&, const WordCount&);
public:
WordCount( string word, int cnt=1 );
// ...
private:
string word;
int occurs;
};
ostream&
operator <<( ostream& os, const WordCount& wd )
{ // ¸ñʽ: <occurs> word
os << "< " << wd.occurs << " > "<< wd.word;
return os;
}
WordCount::WordCount( string word, int cnt=1 )
{}

int main()
{
WordCount wd( "sadness", 12 );
cout << "wd:\n" << wd << endl;
return 0;
}
jieao111 2008-04-08
  • 打赏
  • 举报
回复
还有,不要以说VC6差劲是垃圾来显示自己的水平...有本事你自己做个啊...


还有啥说的,
OenAuth.Net 2008-04-08
  • 打赏
  • 举报
回复
更正一下我上面的解释:
#include <iostream>
#include <string>
using namespace std;
class WordCount {
friend ostream& operator<<(ostream&, const WordCount&);
public:
WordCount(string wordTemp,int cnt=1);//但可以用word,但最好别用。这里可以初始化cntprivate:
string word;
int occurs;
};
ostream& operator <<( ostream& os, const WordCount& wd )
{ // 格式: <occurs> word
os << "< " << wd.occurs << " > "<< wd.word;
return os;
}

//实现构造函数
WordCount::WordCount(string wordTemp,int cnt){//注意:不能将cnt=1
word=wordTemp;
occurs=cnt;
}
int main()
{
WordCount wd( "sadness", 12 );
cout << "wd:\n" << wd << endl;
return 0;
}
OenAuth.Net 2008-04-08
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
class WordCount {
friend ostream& operator<<(ostream&, const WordCount&);
public:
WordCount(string wordTemp,int cnt);
private:
string word;
int occurs;
};
ostream& operator <<( ostream& os, const WordCount& wd )
{ // 格式: <occurs> word
os << "< " << wd.occurs << " > "<< wd.word;
return os;
}
WordCount::WordCount(string wordTemp,int cnt){
word=wordTemp;
occurs=cnt;
}
int main()
{
WordCount wd( "sadness", 12 );
cout << "wd:\n" << wd << endl;
return 0;
}


你错在下面几个方面:
一、没有实现构造函数;
二、构造函数中不能使用默认参数;WordCount( string word, int cnt=1 )不能将cnt=1;
三、构造函数中的参数不能使用类成员变量,WordCount( string word, int cnt=1 )不能用word
kesaihao862 2008-04-08
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
class WordCount {
friend ostream&
operator<<(ostream&, const WordCount&);
public:
WordCount( string word, int cnt=1 );
// ...
private:
string word;
int occurs;
};
ostream&
operator <<( ostream& os, const WordCount& wd )
{ // ¸ñʽ: <occurs> word
os << "< " << wd.occurs << " > "<< wd.word;
return os;
}
WordCount::WordCount( string word, int cnt=1 )
{}

int main()
{
WordCount wd( "sadness", 12 );
cout << "wd:\n" << wd << endl;
return 0;
}



根据楼上几位的建议,我改了一下,可是还是通不过..

还有,不要以说VC6差劲是垃圾来显示自己的水平...有本事你自己做个啊...
yashiro_ 2008-04-08
  • 打赏
  • 举报
回复
编译不通过的原因是你的构造函数没有定义!

WordCount( string word, int cnt=1 );


加上定义就行了。
Supper_Jerry 2008-04-08
  • 打赏
  • 举报
回复
做成inline或者放到内部去

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧