由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - please help debug this code
相关主题
c++ template question:0 < -1 ? A c++ question
about namespace小问题
c++ iterator 弱问a question about bitwise operation
Why should i include .cpp instead of .h一个C++的概念问题
partial_sort问题呼唤大侠们,我实在不能实现C++泛型的精神。
C++重载<<错误?ofstream and cout question
请问关于overloading <<在帮忙看看这个吧 C: int->char*
[C++] 新手报错求助C++ Q13: Input
相关话题的讨论汇总
话题: ostream话题: namespace话题: iterator话题: tentry话题: string
进入Programming版参与讨论
1 (共1页)
l*********s
发帖数: 5409
1
MSVC老是抱怨没有《右边是TEntry类型的重载,但是明明一开始就重载了啊,到底怎么
回事请大家看看。
(这段代码来自于Think in C++)
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef map> Thesaurus;
typedef pair> TEntry;
typedef Thesaurus::iterator TIter;
ostream& operator<<(ostream& os, const TEntry& t){
os << t.first <<": ";
ostream_iterator out_it(os," ");
copy(t.second.begin(),t.second.end(),out_it);
return os;
}
class ThesaurusGen{
static const string letters;
static int count;
public:
int maxSize() { return letters.size(); }
ThesaurusGen(){ srand(time(0)); }
TEntry operator()(){
TEntry result;
if(count>=maxSize()) count=0;
result.first=letters[count++];
int entries=(rand() % 5)+2;
for(int i=0;i int choice=rand()% maxSize();
char cbuf[2]={0};
cbuf[0]=letters[choice];
result.second.push_back(cbuf);
}
return result;
}
};
int ThesaurusGen::count=0;
const string ThesaurusGen::letters("
ABCDEFGHIJKLMNOPQRSTUVWZYZabcdefghijklmnopqrsstuvwxyz");
int main(){
Thesaurus thesaurus;
generate_n( inserter(thesaurus, thesaurus.begin()),10,ThesaurusGen());
ostream_iterator out_it(cout,"\n");
copy(thesaurus.begin(),thesaurus.end(),out_it);
while(true){
cout<<"Select a "word",0 to quit:";
for(TIter it=thesaurus.begin(); it != thesaurus.end(); it++)
cout<<(*it).first<<" ";
cout< string reply;
cin >> reply;
if(reply.at(0) == '0' ) return 0;
if(thesaurus.find(reply) == thesaurus.end())
continue;
vector& v=thesaurus[reply];
copy( v.begin(),v.end(),ostream_iterator(cout," "));
cout< }
} // /:~

t****t
发帖数: 6806
2
where is error message?
such a long program, you want reader to install a VC for this?

【在 l*********s 的大作中提到】
: MSVC老是抱怨没有《右边是TEntry类型的重载,但是明明一开始就重载了啊,到底怎么
: 回事请大家看看。
: (这段代码来自于Think in C++)
: #include
: #include
: #include
: #include
: #include
: #include
: #include

l*********s
发帖数: 5409
3
c:\program files\microsoft visual studio 9.0\vc\include\iterator(308) :
error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'const TEntry' (or there is no acceptable conversion)
c:\program files\microsoft visual studio 9.0\vc\include\iterator(307)
: while compiling class template member function 'std::ostream_iterator<_Ty
> &std::ostream_iterator<_Ty>::operator =(const _Ty &)'
1> with
1> [
1> _Ty=TEntry
1> ]
1> c:\documents and settings\gz\my documents\visual studio 2008\
projects\test3\test3\test3.cpp(58) : see reference to class template
instantiation 'std::ostream_iterator<_Ty>' being compiled
1> with
1> [
1> _Ty=TEntry
1> ]
不好意思,这个VC的错误也很长的说

【在 t****t 的大作中提到】
: where is error message?
: such a long program, you want reader to install a VC for this?

t****t
发帖数: 6806
4
interesting enough, it seems that ostream_iterator will not consider
overloaded operator<< in global namespace. i tried on gcc 4.4.4 and 4.6.0,
they both failed. however if I overload the operator<< in namespace std, it
can pass. And if you do not invoke the operator<< in ostream_iterator, it is
ok as well, no matter which namespace you use.
I am not sure whether this is a more strict constraint that existed long ago
but not enforced, or this is a bug. and interesting enough, both gcc and VC
has this bug??? probably not likely...

【在 l*********s 的大作中提到】
: MSVC老是抱怨没有《右边是TEntry类型的重载,但是明明一开始就重载了啊,到底怎么
: 回事请大家看看。
: (这段代码来自于Think in C++)
: #include
: #include
: #include
: #include
: #include
: #include
: #include

l*********s
发帖数: 5409
5
OoOo, 那估计就是Brue Eckel写书的时候标准里有些限制,编译器没有实现了。

it
is
ago
VC

【在 t****t 的大作中提到】
: interesting enough, it seems that ostream_iterator will not consider
: overloaded operator<< in global namespace. i tried on gcc 4.4.4 and 4.6.0,
: they both failed. however if I overload the operator<< in namespace std, it
: can pass. And if you do not invoke the operator<< in ostream_iterator, it is
: ok as well, no matter which namespace you use.
: I am not sure whether this is a more strict constraint that existed long ago
: but not enforced, or this is a bug. and interesting enough, both gcc and VC
: has this bug??? probably not likely...

t****t
发帖数: 6806
6
OK, solved. let me explain this, this is complex.
you called copy(....begin(), ....end(), ostream_iterator<...>(...)), so
ostream_iterator will do the << for you. naturally, ostream_iterator is
within namespace std, so operator<< is called in namespace std.
Now for overload resolution. there are a lot of operator<< within namespace
std; so these are considered. your parameters to operator<< are std::basic_
ostream<...> and std::pair<...>, they are both in namespace std. Therefore
no other namespaces are considered. This means your definition in global
namespace is not considered.
Now if on the other hand, one of your parameters of operator<< is your own
class, let's say X, within global namespace, then global namespace will be
considered for function overload resolution. for example:
#include
#include
#include
struct X {
int x;
};
using namespace std;
ostream& operator<<(ostream& os, const X& x)
{
os< return os;
}
int main()
{
set a;
copy(a.begin(), a.end(), ostream_iterator(cout, " "));
}
This will be OK. However if you define X inside namespace A, like this:
#include
#include
#include
namespace A {
struct X {
int x;
};
}
using namespace std;
using namespace A;
ostream& operator<<(ostream& os, const X& x)
{
os< return os;
}
int main()
{
set a;
copy(a.begin(), a.end(), ostream_iterator(cout, " "));
}
This will not pass, unless you define the operator<< in either namespace std
, or namespace A.
Reference:
http://stackoverflow.com/questions/5195512/namespaces-and-opera

it
is
ago
VC

【在 t****t 的大作中提到】
: interesting enough, it seems that ostream_iterator will not consider
: overloaded operator<< in global namespace. i tried on gcc 4.4.4 and 4.6.0,
: they both failed. however if I overload the operator<< in namespace std, it
: can pass. And if you do not invoke the operator<< in ostream_iterator, it is
: ok as well, no matter which namespace you use.
: I am not sure whether this is a more strict constraint that existed long ago
: but not enforced, or this is a bug. and interesting enough, both gcc and VC
: has this bug??? probably not likely...

X****r
发帖数: 3557
7
Nice explanation.
To put it simply, name lookup occurs before overload resolution.
As soon as the compiler find the name in the 'right' scope, it
will only try to get the function with the right signature from
this scope.
A minimal example:
void f(int) {}
namespace n {
void f() {}
void g() {
f(1); // will not compile, unless void f(){} is removed.
}
} // namespace n
(Of course, there is more than this, but thrust already explained
in detail)

namespace

【在 t****t 的大作中提到】
: OK, solved. let me explain this, this is complex.
: you called copy(....begin(), ....end(), ostream_iterator<...>(...)), so
: ostream_iterator will do the << for you. naturally, ostream_iterator is
: within namespace std, so operator<< is called in namespace std.
: Now for overload resolution. there are a lot of operator<< within namespace
: std; so these are considered. your parameters to operator<< are std::basic_
: ostream<...> and std::pair<...>, they are both in namespace std. Therefore
: no other namespaces are considered. This means your definition in global
: namespace is not considered.
: Now if on the other hand, one of your parameters of operator<< is your own

l*********s
发帖数: 5409
8
叹为观止,楼上两位太牛叉了!
z****e
发帖数: 2024
9
那是师傅们,学着点!

【在 l*********s 的大作中提到】
: 叹为观止,楼上两位太牛叉了!
h***o
发帖数: 5030
10
thrust you are so NB

it
is
ago
VC

【在 t****t 的大作中提到】
: interesting enough, it seems that ostream_iterator will not consider
: overloaded operator<< in global namespace. i tried on gcc 4.4.4 and 4.6.0,
: they both failed. however if I overload the operator<< in namespace std, it
: can pass. And if you do not invoke the operator<< in ostream_iterator, it is
: ok as well, no matter which namespace you use.
: I am not sure whether this is a more strict constraint that existed long ago
: but not enforced, or this is a bug. and interesting enough, both gcc and VC
: has this bug??? probably not likely...

l*********s
发帖数: 5409
11
nod nod

【在 z****e 的大作中提到】
: 那是师傅们,学着点!
1 (共1页)
进入Programming版参与讨论
相关主题
C++ Q13: Inputpartial_sort问题
stl container erase in a loopC++重载<<错误?
[C++ boost::interprocess] 讨论贴请问关于overloading <<
请教C++程序中的Maze().exitMaze();[C++] 新手报错求助
c++ template question:0 < -1 ? A c++ question
about namespace小问题
c++ iterator 弱问a question about bitwise operation
Why should i include .cpp instead of .h一个C++的概念问题
相关话题的讨论汇总
话题: ostream话题: namespace话题: iterator话题: tentry话题: string