G
goosen_cug
This program is a "Sequential List" class I want to do the Union
Operation,Intersection Operation of the
Set.But this program have a problem:
///////////////////////////
Compiling...
Set.cpp
H:\cheung\Set\Set.cpp(81) : error C2664: 'Insert' : cannot convert
parameter 1 from 'int' to 'int &'
A reference that is not to 'const' cannot be bound to a
non-lvalue
Error executing cl.exe.
Set.exe - 1 error(s), 0 warning(s)""""""""
////////////////////////////////
#include<iostream>
using namespace std;
int x;
class SeqList
{
int *data;
int MaxSize;
int last; public:
SeqList ( int defaultSize );
~SeqList ( ) { delete [ ] data; }
int const Length ( ) { return last; }
int Find ( int & x ) const;
int IsIn ( int & x );
int Insert ( int&x, int i );
int Remove ( int & x );
int IsEmpty ( ) { return last ==-1; }
int IsFull ( ) { return last == MaxSize-1; }
int Get ( int i )
{
return i < 0 || i > last? NULL : data;
}
void print();
};
SeqList :: SeqList ( int sz ) {
if ( sz > 0 ) {
MaxSize = sz; last = -1;
data = new int[MaxSize];
if ( data == NULL ) {
MaxSize = 0; last = -1;
return;
}
}
}
int SeqList::Find ( int & x ) const {
int i = 0;
while ( i <= last && data != x )
i++;
if ( i > last ) return -1;
else return i;
}
int SeqList::Insert ( int&x, int i )
{
if ( i < 0 || i > last+1 || last == MaxSize-1 )
return 0;
else {
last++;
for ( int j = last; j > i; j-- )
data[j] = data[j -1];
data = x;
return 1;
}
}
int SeqList::Remove ( int&x ) {
int i = Find (x);
if ( i >= 0 ) {
last-- ;
for ( int j = i; j <= last; j++ )
data[j] = data[j+1];
return 1;
}
return 0;
}
void SeqList:rint()
{
for(int i=0;i<last;i++)
cout<<data<<",";
}
void Union ( SeqList & LA,SeqList & LB )
{
int n = LA.Length ( );
int m = LB.Length ( );
for ( int i = 1; i <= m; i++ ) {
int x = LB.Get(i);
int k = LA.Find (x);
if ( k == -1 )
{
LA.Insert (n+1, x);
n++;
}
}
}
void Intersection ( SeqList & LA,SeqList & LB )
{
int n = LA.Length ( );
int m = LB.Length ( ); int i = 0;
while ( i < n )
{
int x = LA.Get (i);
int k = LB.Find (x);
if ( k == -1 )
{
LA.Remove (i); n--;
}
else i++; }
}
//void Subtration(SeqList & LA,SeqList & LB )
void main()
{
int *Array1,*Array2;
Array1=new int [x];
Array2=new int [x];
cout<<"please out in the elements of Set A"<<endl;
cin>>*Array1;
cout<<"please out in the elements of Set B"<<endl;
cin>>*Array2;
int y=2*x;
SeqList A(y),B(x);
for(int i=0;i<y;i++)
{
A.Insert(Array1,i);
B.Insert(Array2,i);
}
Intersection(A,B);
A.print();
}
How to improve this problem?
Thank you !
Operation,Intersection Operation of the
Set.But this program have a problem:
///////////////////////////
Compiling...
Set.cpp
H:\cheung\Set\Set.cpp(81) : error C2664: 'Insert' : cannot convert
parameter 1 from 'int' to 'int &'
A reference that is not to 'const' cannot be bound to a
non-lvalue
Error executing cl.exe.
Set.exe - 1 error(s), 0 warning(s)""""""""
////////////////////////////////
#include<iostream>
using namespace std;
int x;
class SeqList
{
int *data;
int MaxSize;
int last; public:
SeqList ( int defaultSize );
~SeqList ( ) { delete [ ] data; }
int const Length ( ) { return last; }
int Find ( int & x ) const;
int IsIn ( int & x );
int Insert ( int&x, int i );
int Remove ( int & x );
int IsEmpty ( ) { return last ==-1; }
int IsFull ( ) { return last == MaxSize-1; }
int Get ( int i )
{
return i < 0 || i > last? NULL : data;
}
void print();
};
SeqList :: SeqList ( int sz ) {
if ( sz > 0 ) {
MaxSize = sz; last = -1;
data = new int[MaxSize];
if ( data == NULL ) {
MaxSize = 0; last = -1;
return;
}
}
}
int SeqList::Find ( int & x ) const {
int i = 0;
while ( i <= last && data != x )
i++;
if ( i > last ) return -1;
else return i;
}
int SeqList::Insert ( int&x, int i )
{
if ( i < 0 || i > last+1 || last == MaxSize-1 )
return 0;
else {
last++;
for ( int j = last; j > i; j-- )
data[j] = data[j -1];
data = x;
return 1;
}
}
int SeqList::Remove ( int&x ) {
int i = Find (x);
if ( i >= 0 ) {
last-- ;
for ( int j = i; j <= last; j++ )
data[j] = data[j+1];
return 1;
}
return 0;
}
void SeqList:rint()
{
for(int i=0;i<last;i++)
cout<<data<<",";
}
void Union ( SeqList & LA,SeqList & LB )
{
int n = LA.Length ( );
int m = LB.Length ( );
for ( int i = 1; i <= m; i++ ) {
int x = LB.Get(i);
int k = LA.Find (x);
if ( k == -1 )
{
LA.Insert (n+1, x);
n++;
}
}
}
void Intersection ( SeqList & LA,SeqList & LB )
{
int n = LA.Length ( );
int m = LB.Length ( ); int i = 0;
while ( i < n )
{
int x = LA.Get (i);
int k = LB.Find (x);
if ( k == -1 )
{
LA.Remove (i); n--;
}
else i++; }
}
//void Subtration(SeqList & LA,SeqList & LB )
void main()
{
int *Array1,*Array2;
Array1=new int [x];
Array2=new int [x];
cout<<"please out in the elements of Set A"<<endl;
cin>>*Array1;
cout<<"please out in the elements of Set B"<<endl;
cin>>*Array2;
int y=2*x;
SeqList A(y),B(x);
for(int i=0;i<y;i++)
{
A.Insert(Array1,i);
B.Insert(Array2,i);
}
Intersection(A,B);
A.print();
}
How to improve this problem?
Thank you !