l*********s 发帖数: 5409 | 1 MSVC老是抱怨没有《右边是TEntry类型的重载,但是明明一开始就重载了啊,到底怎么
回事请大家看看。
(这段代码来自于Think in C++)
#include
#include | 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
| 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
| 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 | | 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 的大作中提到】 : 那是师傅们,学着点!
|
|