简单的操作符重载问题

staryshine2008 2004-06-22 02:57:29
#include <iostream>
#include <string>

class WordCount
{
friend ostream& operator<<(ostream&,const WordCount&);
public:
WordCount(string word,int cnt)
:_word(word),_occurs(cnt)
{}
private:
string _word;
int _occurs;
};
ostream& WordCount::operator<< (ostream& os,const WordCount& wd)
{
os << "<" << wd._occurs << ">" << wd._word;
return os;
}

///////////
#include <iostream>
#include <string>
#include "WordCount.h"
using namespace std;
int main()
{
WordCount wd("sadness",12);
cout << "wd:\n" <<wd <<endl;
return 0;
}
///////////
看不出哪里错了,编译的时候出现了若干错误
:\code_cplus\cplus\wordcount.h(6) : error C2143: syntax error : missing ';' before '&'
d:\code_cplus\cplus\wordcount.h(6) : error C2433: 'ostream' : 'friend' not permitted on data declarations
d:\code_cplus\cplus\wordcount.h(6) : error C2501: 'ostream' : missing storage-class or type specifiers
d:\code_cplus\cplus\wordcount.h(6) : error C2244: 'ostream' : unable to resolve function overload
d:\code_cplus\cplus\wordcount.h(6) : error C2061: syntax error : identifier 'ostream'
d:\code_cplus\cplus\wordcount.h(6) : error C2501: '<<' : missing storage-class or type specifiers
d:\code_cplus\cplus\wordcount.h(6) : error C2805: binary 'operator <<' has too few parameters
d:\code_cplus\cplus\wordcount.h(8) : error C2629: unexpected 'class WordCount ('
d:\code_cplus\cplus\wordcount.h(8) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
d:\code_cplus\cplus\wordcount.h(12) : error C2146: syntax error : missing ';' before identifier '_word'
d:\code_cplus\cplus\wordcount.h(12) : error C2501: 'string' : missing storage-class or type specifiers
d:\code_cplus\cplus\wordcount.h(12) : error C2501: '_word' : missing storage-class or type specifiers
d:\code_cplus\cplus\wordcount.h(15) : error C2143: syntax error : missing ';' before '&'
d:\code_cplus\cplus\wordcount.h(15) : error C2501: 'ostream' : missing storage-class or type specifiers
d:\code_cplus\cplus\wordcount.h(15) : error C2061: syntax error : identifier 'ostream'
d:\code_cplus\cplus\wordcount.h(16) : error C2501: '<<' : missing storage-class or type specifiers
d:\code_cplus\cplus\wordcount.h(17) : error C2065: 'os' : undeclared identifier
d:\code_cplus\cplus\wordcount.h(17) : error C2297: '<<' : illegal, right operand has type 'char [2]'
d:\code_cplus\cplus\wordcount.h(17) : error C2065: 'wd' : undeclared identifier
d:\code_cplus\cplus\wordcount.h(17) : error C2228: left of '._occurs' must have class/struct/union type
d:\code_cplus\cplus\wordcount.h(17) : error C2228: left of '._word' must have class/struct/union type
D:\Code_Cplus\CPLUS\output.cpp(9) : error C2661: 'WordCount::WordCount' : no overloaded function takes 2 parameters
D:\Code_Cplus\CPLUS\output.cpp(10) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class WordCount' (or there is no acceptable conversion)
Error executing cl.exe.

CPLUS.exe - 23 error(s), 0 warning(s)
请大侠们指教



...全文
163 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
staryshine2008 2004-07-01
  • 打赏
  • 举报
回复
************************************************************
WordCount(string word,int cnt)
:_word(word),_occurs(cnt)
{}
word(word),string类型是不能这样赋初值的.
************************************************************
??为什么不行呢,string类不是有自己的拷贝构造函数吗??
staryshine2008 2004-07-01
  • 打赏
  • 举报
回复
************************************************************
WordCount(string word,int cnt)
:_word(word),_occurs(cnt)
{}
word(word),string类型是不能这样赋初值的.
************************************************************
??为什么不行呢,string类不是有自己的拷贝构造函数吗??
Hillside 2004-06-23
  • 打赏
  • 举报
回复
WordCount(string word,int cnt)
:_word(word),_occurs(cnt)
{}
word(word),string类型是不能这样赋初值的

ostream& WordCount::operator<< (ostream& os,const WordCount& wd)
WordCount::operator改为operator,因为operator并不是WordCount的成员。
alpha15 2004-06-22
  • 打赏
  • 举报
回复
up
lsdkzkald 2004-06-22
  • 打赏
  • 举报
回复
还有忘记说了

还要带上
using namespace std;
tbwisess 2004-06-22
  • 打赏
  • 举报
回复
如果把file1和file2 "using namespce std;"注释去掉,那么关键就可以把含有using std::的所有行去掉。

均编译运行通过:vc.net
所以你的wordcount.h有两个问题:
1,header没有std空间声明,仅仅包含iostream和string是8够的(vc下)
2,::用错了
tbwisess 2004-06-22
  • 打赏
  • 举报
回复
//file::"tmp.h"
#include <iostream>
#include <string>
//using namespace std;

class WordCount
{
friend std::ostream& operator<<(std::ostream&,const WordCount&);

public:
WordCount(std::string word,int cnt)
:_word(word),_occurs(cnt)
{}
private:
std::string _word;
int _occurs;
};

std::ostream& operator<< (std::ostream& os,const WordCount& wd)
{
os << "<" << wd._occurs << ">" << wd._word;
return os;
}
//file2
#include "tmp.h"
//using namespace std;
using std::endl;
using std::cout;
int main()
{
WordCount wd("sadness",12);
cout << "wd:\n" <<wd <<endl;
return 0;
}
lsdkzkald 2004-06-22
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
class WordCount
{
private:
string _word;
int _occurs;

public:
WordCount(string word,int cnt)
:_word(word),_occurs(cnt)
{}
friend ostream& operator<<(ostream&,const WordCount&);
};
ostream& operator<< (ostream& os,const WordCount& wd)//友元应该不用WordCount::
{
os << "<" << wd._occurs << ">" << wd._word;
return os;
}



#include "121212.h"
int main()
{
WordCount wd("sadness",12);
cout << "wd:\n" <<wd <<endl;
return 0;
}
prf8 2004-06-22
  • 打赏
  • 举报
回复
不过对c++支持的最好的还是DEV-c++
prf8 2004-06-22
  • 打赏
  • 举报
回复
vc6有升级包了。
staryshine2008 2004-06-22
  • 打赏
  • 举报
回复
我把WordCount wd("sadness",12);这句话改成WordCount wd("sadness");提示出错(可是我提供的第二个参数有一个缺省值呀??为什么??)然后改回来WordCount wd("sadness",12);结果又出了一大堆的错误,这次用的是dev-c++编译的。
staryshine2008 2004-06-22
  • 打赏
  • 举报
回复
真是奇怪,在VC6里面都声明了友元了,在操作符重载的时候还提示我不能调用类的私有成员。看来还是dev-c++好用,可惜他没有类的成员函数提示。
cxc014 2004-06-22
  • 打赏
  • 举报
回复
如果需要使用vc6的话,需要把私有变量改为public

我没有改也通过了~~~~~~~
flyerlxg 2004-06-22
  • 打赏
  • 举报
回复
study and help you up
qwertasdfg123 2004-06-22
  • 打赏
  • 举报
回复
如果需要使用vc6的话,需要把私有变量改为public。
cxc014 2004-06-22
  • 打赏
  • 举报
回复
VC中:头文件中改ostream为std::ostream

改ostream& WordCount::operator<< (ostream& os,const WordCount& wd)
{
os << "<" << wd._occurs << ">" << wd._word;
return os;
}为:
ostream& operator<< (ostream& os,const WordCount& wd)
{
os << "<" << wd._occurs << ">" << wd._word;
return os;
}
调试通过~~~~



qwertasdfg123 2004-06-22
  • 打赏
  • 举报
回复
vc对<<支持的问题。使用gcc。
还可以屏蔽use namespace std;
改为std::。
如:
friend std::ostream& operator<<(std::ostream&,const WordCount&);
kaphoon 2004-06-22
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

class WordCount
{
friend ostream& operator<<(ostream&,const WordCount&);

public:
WordCount(string word,int cnt)
:_word(word),_occurs(cnt)
{}
private:
string _word;
int _occurs;
};

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;
}
//编译通过
staryshine2008 2004-06-22
  • 打赏
  • 举报
回复
问题还是存在的,怎么办呀,没人回答了……
friend ostream& operator<<(ostream&,const WordCount&);
//换成这个声明还是有上面的错误
staryshine2008 2004-06-22
  • 打赏
  • 举报
回复
不好意思,这个错误我已经改正过来了,忘了存盘
ostream& WordCount::operator<< (ostream& os,const WordCount& wd)//不是友元


friend ostream& operator<<(ostream&,const WordCount&);换成这个声明还是有上面的错误
加载更多回复(3)

64,643

社区成员

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

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