| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 531 人关注过本帖
标题:[求助]重载
只看楼主 加入收藏
cwq2006
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-11-15
收藏
 问题点数:0 回复次数:4 
[求助]重载

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


class Location {
friend ostream& operator<<( ostream&, const Location& );
public:
Location( int line, int col )
: _line( line ), _col( col ) {}
private:
short _line;
short _col;
};

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

public:
WordCount() {}
WordCount( string word ) : _word( word ) {}
WordCount( string word, int ln, int col )
: _word( word ){ insert_location( ln, col ); }

string word() const { return _word; }
int occurs() const { return _occurList.size(); }
void found( int ln, int col )
{ insert_location( ln, col ); }

private:
void insert_location( int ln, int col )
{ _occurList.push_back( Location( ln, col )); }

string _word;
vector< Location > _occurList;
};

ostream& operator <<( ostream& os, const Location& lc )
{
// output of a Loc object: < 10,37 >
os << "<" << lc._line
<< "," << lc._col << "> ";

return os;
}

ostream&
operator <<( ostream& os, const WordCount& wd )
{
os << "<" << wd._occurList.size() << "> "
<< wd._word << endl;

int cnt = 0, onLine = 6;
vector<Location>::const_iterator first = wd._occurList.begin();
vector<Location>::const_iterator last = wd._occurList.end();

for ( ; first != last; ++first )
{
// os << Location
os << *first << " ";

// formatting: 6 to a line
if ( ++cnt >= onLine )
{ os << "\n"; cnt = 0; }

}
return os;
}

int main()
{
WordCount search( "rosebud" );

// for simpilicity, hand code 8 occurrences
search.found(11,3); search.found(11,8);
search.found(14,2); search.found(34,6);
search.found(49,7); search.found(67,5);
search.found(81,2); search.found(82,3);
search.found(91,4); search.found(97,8);

cout << "Occurrences: " << "\n"
<< search << endl;

return 0;
}

输出结果应该是这样的:

/**
**
Occurrences:
<10> rosebud
<11,3> <11,8> <14,2> <34,6> <49,7> <67,5>
<81,2> <82,3> <91,4> <97,8>
**
**/

编译时8个报错

-------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
d:\test\test.cpp(44) : error C2248: '_line' : cannot access private member declared in class 'Location'
d:\test\test.cpp(13) : see declaration of '_line'
d:\test\test.cpp(44) : error C2248: '_col' : cannot access private member declared in class 'Location'
d:\test\test.cpp(14) : see declaration of '_col'
d:\test\test.cpp(52) : error C2248: '_occurList' : cannot access private member declared in class 'WordCount'
d:\test\test.cpp(37) : see declaration of '_occurList'
d:\test\test.cpp(53) : error C2248: '_word' : cannot access private member declared in class 'WordCount'
d:\test\test.cpp(36) : see declaration of '_word'
d:\test\test.cpp(56) : error C2248: '_occurList' : cannot access private member declared in class 'WordCount'
d:\test\test.cpp(37) : see declaration of '_occurList'
d:\test\test.cpp(57) : error C2248: '_occurList' : cannot access private member declared in class 'WordCount'
d:\test\test.cpp(37) : see declaration of '_occurList'
d:\test\test.cpp(62) : error C2593: 'operator <<' is ambiguous
d:\test\test.cpp(84) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

test.exe - 8 error(s), 0 warning(s)

搜索更多相关主题的帖子: 重载 
2006-11-15 14:00
smartwind
Rank: 1
等 级:新手上路
威 望:1
帖 子:277
专家分:0
注 册:2006-11-13
收藏
得分:0 
不知道你是用的什么编译器
我用vc++一个错误都没有

2006-11-17 17:19
forever043
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-10-15
收藏
得分:0 
回复:(cwq2006)[求助]重载
也遇到过同样的错误,
如果把#include <iostream>
改为#include <iostream.h>
就可以了,
但不清楚为什么用std名称空间的就不行

学操作系统真的好郁闷啊.........
2006-11-22 22:11
朦朦胧胧
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-9-19
收藏
得分:0 

似乎是编译器的一个bug,把<<的定义放到类定义的里面,如下:

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

class Location {
friend ostream& operator <<( ostream& os, const Location& lc )
{
// output of a Loc object: < 10,37 >
os << "<" << lc._line
<< "," << lc._col << "> ";

return os;
}
public:
Location( int line, int col )
: _line( line ), _col( col ) {}
private:
short _line;
short _col;
};

class WordCount {
friend ostream& operator <<( ostream& os, const WordCount& wd )
{
os << "<" << wd._occurList.size() << "> "
<< wd._word << endl;

int cnt = 0, onLine = 6;
vector<Location>::const_iterator first = wd._occurList.begin();
vector<Location>::const_iterator last = wd._occurList.end();

for ( ; first != last; ++first )
{
// os << Location
os << *first << " ";

// formatting: 6 to a line
if ( ++cnt >= onLine )
{ os << "\n"; cnt = 0; }

}
return os;
}
friend istream& operator>>( istream&, WordCount& );

public:
WordCount() {}
WordCount( string word ) : _word( word ) {}
WordCount( string word, int ln, int col )
: _word( word ){ insert_location( ln, col ); }

string word() const { return _word; }
int occurs() const { return _occurList.size(); }
void found( int ln, int col )
{ insert_location( ln, col ); }

private:
void insert_location( int ln, int col )
{ _occurList.push_back( Location( ln, col )); }

string _word;
vector< Location > _occurList;
};

int main()
{
WordCount search( "rosebud" );

// for simpilicity, hand code 8 occurrences
search.found(11,3); search.found(11,8);
search.found(14,2); search.found(34,6);
search.found(49,7); search.found(67,5);
search.found(81,2); search.found(82,3);
search.found(91,4); search.found(97,8);

cout << "Occurrences: " << "\n"
<< search << endl;

return 0;
}


2006-11-23 13:00
linghao
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2006-11-20
收藏
得分:0 
标准C++头文件&lt;iostream&gt;
2006-11-24 18:16
快速回复:[求助]重载
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016593 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved