error C2593: 'operator <<' is ambiguous,怎么解决?

C++语言 码拜 9年前 (2015-05-11) 3409次浏览 0个评论
 

我用的是vc6.0。如果把#include <stream>改成#include<iostream.h>并删去using namespace std;就会出现大量错误。怎么解决?#include <iostream.h>
#include <string>
using namespace std;

class student
{
public:
student(int num1,string nam,char s,int age1,float m,float c,float f):num(num1),sex(s),age(age1),math_score(m),c_score(c), fg_score(f)
{
     name=nam;
}

void show()
{
cout<<num<<“” “”<<name<<“” “”<<sex<<“” “”<<age<<“” “”<<math_score<<“” “”<<c_score<<“” “”<<fg_score<<endl;
}
friend ostream& operator <<(ostream&,student&);
friend istream& operator >>(istream&,student&);

int num;
string name;
char sex;
int age;
float math_score,c_score,fg_score;
   
};

ostream& operator <<(ostream&output,student&stu)
{
output<<stu.num <<“” “”<<stu.name <<“” “”<<stu.sex <<“” “”<<stu.age <<“” “”<<stu.math_score <<“” “”<<stu.c_score <<“” “”<<stu.fg_score <<endl;
return output;
}

istream& operator >>(istream&input,student&stu)
{
    input>>stu.num >>stu.name >>stu.sex >>stu.age >>stu.math_score >>stu.c_score>>stu.fg_score ;
return input;
}

int main()
{
student stu[2]={student(1,”wangping”,””m””,18,89.0,78.9,96.8),student(2,”lihua”,””f””,19,89.0,67.9,99.0)};
stu[0].show ();
stu[1].show ();
cout<<stu[1];
return 0;

}

#include <iostream.h>

换成

#include <iostream>

试试。

10分
同意楼上的说法。<iostream.h>不是标准C++里面的头文件,而<string>则包含在标准里面,所以删去using namespace std后,类里面的数据成员string name无法明确其类型。
建议把<iostream.h>改成<iostream>,同时头文件不建议用using namespace std,可以把string name改成std::string name
iostream.h这样的写法就不用在考虑了。
不要以为
#include <iostream>
这样写有多神秘!
其实就是包含include目录下的文件“iostream”而已,

#include <stdio.h>
包含include目录下的文件“stdio.h”类似。
引用 2 楼 formydream1 的回复:

同意楼上的说法。<iostream.h>不是标准C++里面的头文件,而<string>则包含在标准里面,所以删去using namespace std后,类里面的数据成员string name无法明确其类型。
建议把<iostream.h>改成<iostream>,同时头文件不建议用using namespace std,可以把string name改成std::str……

using namespace std;是什么?我目前只是按照书上的写的,却不知道他的意义?不用行吗?不需要别的什么来代替吗?

引用 4 楼 zhao4zhong1 的回复:

不要以为
#include <iostream>
这样写有多神秘!
其实就是包含include目录下的文件“iostream”而已,

#include <stdio.h>
包含include目录下的文件“stdio.h”类似。

如果开头这样写:#include <iostream>
using namespace std;
它就说运算符<<歧义怎么解决
我查书说改成iostream.h并去掉using namespace  std;
这样有不能用sting定义参数name.

引用 5 楼 fjn2012 的回复:

引用 2 楼 formydream1 的回复:同意楼上的说法。<iostream.h>不是标准C++里面的头文件,而<string>则包含在标准里面,所以删去using namespace std后,类里面的数据成员string name无法明确其类型。
建议把<iostream.h>改成<iostream>,同时头文件不建议用using namespace std,可……

命名空间,建议你看看c++primer里面的相关章节,using namespace std是一种偷懒省事的方式,这样你可以直接写string,如果没写那个,你就写std::string,效果是一样的
命名空间的目的是为了防止命名污染,也就是说你可以你一个cpp文件中,有两个全局变量都成为var,这样会编译报错。当如果你用命名空间,namesapce A{int var;},namespace B{int var;};这样就不会报错了,用的时候就A::var或B::var来区分

10分
《C++primer》  命名空间
哎,每一个有用的

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明error C2593: 'operator <<' is ambiguous,怎么解决?
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!