重载<<时出现关于头文件的问题
#include <iostream>
using namespace std;
class A
{
public:
int b;
public:
A(int i):b(i){}
friend void operator < <(ostream& out,A& m);
};
void operator < <(ostream& out,A& m)
{
out < <m.b < <endl;
};
int main(int argc, char* argv[])
{
A n = 10;
cout < <n; ////在这一行会报 < <存在二意性的错误
return 0;
}
这段代码的错误在上面说明了,但是如果把头文件换成是iostream.h的话就没有问题,可以正常运行,是在VC6下写的。
分别看了下这两个头文件的内容。
iostream.h里包括以下部分内容:
#include <ios.h> // Define ios.
#include <streamb.h> // Define streambuf.
#include <istream.h> // Define istream.
#include <ostream.h> // Define ostream.
而在iostream里反而只有:
#include <istream>
...
extern _CRTIMP istream cin;
extern _CRTIMP ostream cout;
说明在不带h的头文件里没有其他重载 < <的函数,反而是在带h的头文件里包含的那个ostream.h里有很多重载过的 < <函数。如果非要说有二意的话那么不应该是用带h的头文件时更可能产生吗?
[解决办法]
楼上正解
[解决办法]
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q192/5/39.asp&NoWebContent=1