为什么总是编译通不过呢?

underuwing 2009-09-21 07:51:49
题目:
找出给定的n个整数中前k(k<=n)个大的整数。
代码:
/*File:mset.h声明一个M_Set类*/
class M_Set
{
public:
M_Set(int sz);
void Insert(const int& x);
int IsEmpty();
int IsFull();
int Minvalue();
void DeleteMin();
friend ostream& operator<<(ostream&,const M_Set&);
private:
int* elem;
int maxsize,num;
int FindMin();
};


/*File mset.cpp 类M_Set的实现*/
#include <assert.h>
#include "mset.h"
M_Set::M_Set(int sz):num(0)
{
if(sz>0)
{
maxsize = sz;
elem = new int[maxsize];
}
else
cerr<<"the length if SET is error!"<<endl;
}
int M_Set::IsEmpty()
{
if(num==0) return 1;
return 0;
}
int M_Set::IsFull()
{
if(num==maxsize) return 1;
return 0;
}
int M_Set::Minvalue()
{
return elem[FindMin()];
}
void M_Set::Insert(const int& x)
{
assert(!IsFull());
elem[num++]=x;
}
void M_Set::DeleteMin()
{
int loc=FindMin();
for(int i=loc;i<num;i++) elem[i]=elem[i+1];
num--;
}
ostream& operator<<(ostream& out,const M_Set& mset)
{
for(int i=0;i<mset.num;i++) out<<mset.elem[i]<<" ";
return out;
}


/*File: msetapp.cpp */
#include <iostream>
#include "mset.h"
using namespace std;
void fun(int* A,M_Set& M,int n,int k)
{
for(int i=0;i<k;i++) M.Insert(A[i]);
for(i=k;i<n;i++)
if(A[i]>M.Minvalue())
{
M.DeleteMin();
M.Insert(A[i]);
}
}
void main()
{
const int MAX=20;
int X[MAX],k,n;
cout<<"input n";
cin>>n;
cout<<"input k";
cin>>k;
if(k>n)
{
cout<<"error!\n";
exit(1);
}
cout<<"input the random start:";
unsigned seed;
cin>>seed;
srand(seed);
for(int i=0;i<n;i++)
X[i]=rand()%1000;
for(int j=0;j<n;i++)
cout<<X[j]<<" ";
cout<<'\n';
M_Set Y(k);
fun(X,Y,n,k);
cout<<"the result is:"<<endl;
cout<<Y<<endl;
}
但是为什么编译时总是提示:
d:\c\stl数据结构\1.2\mset.h(10) : error C2143: syntax error : missing ';' before '&'
d:\c\stl数据结构\1.2\mset.h(10) : error C2433: 'ostream' : 'friend' not permitted on data declarations
d:\c\stl数据结构\1.2\mset.h(10) : error C2501: 'ostream' : missing storage-class or type specifiers
d:\c\stl数据结构\1.2\mset.h(10) : error C2244: 'ostream' : unable to resolve function overload
d:\c\stl数据结构\1.2\mset.h(10) : error C2061: syntax error : identifier 'ostream'
d:\c\stl数据结构\1.2\mset.h(10) : error C2501: '<<' : missing storage-class or type specifiers
d:\c\stl数据结构\1.2\mset.h(10) : error C2805: binary 'operator <<' has too few parameters
d:\c\stl数据结构\1.2\msetapp.cpp(39) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class M_Set' (or there is no acceptable conversion)
执行 cl.exe 时出错.
是运算符重载出问题了么?我用的是vc6.

...全文
249 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
underuwing 2009-09-22
  • 打赏
  • 举报
回复
FindMin函数:
int M_Set::FindMin()
{
assert(!IsEmpty());
int minvalue,loc;
minvalue=elem[0];
loc=0;
for(int i=1;i<num;i++)
if(elem[i]<minvalue)
{
minvalue=elem[i];
loc=i;
}
return loc;
}
在vc 2008 已编译通过。是vc6编译器的问题。
zgjxwl 2009-09-21
  • 打赏
  • 举报
回复
/*File:mset.h声明一个M_Set类*/ 
#include <iostream>
using namespace std;
class M_Set
{
public:
M_Set(int sz);
void Insert(const int& x);
int IsEmpty();
int IsFull();
int Minvalue();
void DeleteMin();
friend ostream& operator <<(ostream& out,const M_Set& mset)
{
for(int i=0;i <mset.num;i++)
out <<mset.elem[i] <<" ";
return out;
}
private:
int* elem;
int maxsize,num;
int FindMin();
};


/*File mset.cpp 类M_Set的实现*/ 
#include <assert.h>
#include "mset.h"
M_Set::M_Set(int sz):num(0)
{
if(sz>0)
{
maxsize = sz;
elem = new int[maxsize];
}
else
cerr <<"the length if SET is error!" <<endl;
}
int M_Set::IsEmpty()
{
if(num==0) return 1;
return 0;
}
int M_Set::IsFull()
{
if(num==maxsize) return 1;
return 0;
}
int M_Set::Minvalue()
{
return elem[FindMin()];
}
void M_Set::Insert(const int& x)
{
assert(!IsFull());
elem[num++]=x;
}
void M_Set::DeleteMin()
{
int loc=FindMin();
for(int i=loc;i <num;i++) elem[i]=elem[i+1];
num--;
}


/*File: msetapp.cpp */ 
#include <iostream>
#include "mset.h"
using namespace std;
void fun(int* A,M_Set& M,int n,int k)
{
for(int i=0;i <k;i++) M.Insert(A[i]);
for(i=k;i <n;i++)
if(A[i]>M.Minvalue())
{
M.DeleteMin();
M.Insert(A[i]);
}
}
void main()
{
const int MAX=20;
int X[MAX],k,n;
cout <<"input n";
cin>>n;
cout <<"input k";
cin>>k;
if(k>n)
{
cout <<"error!\n";
exit(1);
}
cout <<"input the random start:";
unsigned seed;
cin>>seed;
srand(seed);
for(int i=0;i <n;i++)
X[i]=rand()%1000;
for(int j=0;j <n;i++)
cout <<X[j] <<" ";
cout <<'\n';
M_Set Y(k);
fun(X,Y,n,k);
cout <<"the result is:" <<endl;
cout <<Y <<endl;
}



大致改了下。。。基本有,包含头文件。。以及VC6对友元支持不好,这里有bug,把友元函数的实现写在类里可以解决这个bug,最后是你的FindMin()这个函数没有实现。。。自己补全吧
zhulinjia 2009-09-21
  • 打赏
  • 举报
回复
认不到E文就用中文版的OK?

这样报错你也知道 是错在哪了

snake4 2009-09-21
  • 打赏
  • 举报
回复
问题有2
1.FindMin()函数你没有定义,而且还是private?
2.给vc打sp5补丁。
http://download.microsoft.com/download/vstudio60ent/SP5/Wideband-Full/WIN98Me/EN-US/VS6sp5.exe
na2650945 2009-09-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lixkyx 的回复:]
引用 3 楼 tutu08 的回复:
你用vs2005试试
友元函数VC6处理不好!


正如这位网友说的,友元函数在VC6.0里面确实没有办法编译通过的,这是VC6.0的bug,建议楼主换VC2005来试试。
[/Quote]
VC6对流入流出符的重载有BUG。
lixkyx 2009-09-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tutu08 的回复:]
你用vs2005试试
友元函数VC6处理不好!
[/Quote]

正如这位网友说的,友元函数在VC6.0里面确实没有办法编译通过的,这是VC6.0的bug,建议楼主换VC2005来试试。
underuwing 2009-09-21
  • 打赏
  • 举报
回复
我是这样写的
brookmill 2009-09-21
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
underuwing 2009-09-21
  • 打赏
  • 举报
回复
把iostream放进mset.h中的错误提示变成了:
:\c\stl数据结构\1.2\mset.h(12) : error C2872: 'ostream' : ambiguous symbol
d:\c\stl数据结构\1.2\mset.h(12) : error C2872: 'ostream' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(18) : error C2872: 'cout' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(19) : error C2872: 'cin' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(20) : error C2872: 'cout' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(21) : error C2872: 'cin' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(24) : error C2872: 'cout' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(27) : error C2872: 'cout' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(29) : error C2872: 'cin' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(34) : error C2872: 'cout' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(35) : error C2872: 'cout' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(38) : error C2872: 'cout' : ambiguous symbol
d:\c\stl数据结构\1.2\msetapp.cpp(39) : error C2872: 'cout' : ambiguous symbol
执行 cl.exe 时出错.

msetapp.obj - 1 error(s), 0 warning(s)
brookmill 2009-09-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 underuwing 的回复:]
如果msetapp.cpp中包括了iostream会不会和mset.h中的iostream重复呢?
[/Quote]
不会重复。
这种标准的头文件,多次包含不会出问题。自己的头文件,如果写好了也没问题。
underuwing 2009-09-21
  • 打赏
  • 举报
回复
如果msetapp.cpp中包括了iostream会不会和mset.h中的iostream重复呢?
brookmill 2009-09-21
  • 打赏
  • 举报
回复
需要在mset.h的开头#include <iostream>
或者放在mset.cpp的开头也可以,就像msetapp.cpp那样,在#include "mset.h"的前面
tutu08 2009-09-21
  • 打赏
  • 举报
回复
你用vs2005试试
友元函数VC6处理不好!
brookmill 2009-09-21
  • 打赏
  • 举报
回复
mset.h里面用到了ostream,但是没有包含头文件。
liem 2009-09-21
  • 打赏
  • 举报
回复
是否缺少了头文件?

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧