Why this program is wrong?And how to improve it?

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::print()
{
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 !
 
N

Nindi73

Look I am not completelly sure whether this is your problem.
But if I have a function foo

void foo(int &)

I can do

int x =9;
foo(x)

But I don't think I can do
foo(9)

I maybe wrong , but this maybe ur problem

goosen_cug said:
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::print()
{
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 !
 
N

Nindi73

ok just ried what i though was the problem .. and I get the same error
as you .

You are passing the reference of a temp.


Look I am not completelly sure whether this is your problem.
But if I have a function foo

void foo(int &)

I can do

int x =9;
foo(x)

But I don't think I can do
foo(9)

I maybe wrong , but this maybe ur problem

goosen_cug said:
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::print()
{
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 !
 
D

Dan Bloomquist

goosen_cug said:
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
class SeqList
{ ....
int Insert ( int&x, int i ); ....

LA.Insert (n+1, x);
....
int& is a reference to a variable. if you had:
LA.Insert (n, x);

It would have compiled. ints are small enough that you don't need to
reference them. You are not modifying x in Insert so don't need it.
Write your members like:
int Insert ( int x, int i );
How to improve this problem?

In english, you ask to make the problem bigger! :)

Best, Dan.
 
G

goosen_cug

"Dan Bloomquist дµÀ£º
"
...
int& is a reference to a variable. if you had:
LA.Insert (n, x);

It would have compiled. ints are small enough that you don't need to
reference them. You are not modifying x in Insert so don't need it.
Write your members like:
int Insert ( int x, int i );


In english, you ask to make the problem bigger! :)

Best, Dan.
Although there are not problem,it can not run ! thank you
 
B

BobR

goosen_cug wrote in message
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)""""""""
How to improve this problem?
Thank you !


////////////////////////////////
#include<iostream>
using namespace std;
int x;

// > void main() // non-existant
int main(){

int *Array1,*Array2;
Array1=new int [x];
Array2=new int [x];

cout<<" x="<< x <<endl;

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;

cout<<" y="<< y <<endl;

} // main()

Get that to work first. And fix your potential memory leak.
 
D

Dan Bloomquist

goosen_cug said:
Although there are not problem,it can not run !

I didn't try to run it.

Just pasted and ran.
~SeqList ( ) { delete [ ] data; }

data is a garbage pointer. Didn't you see that? Otherwise, I'm sorry not
to fix your program...

Best, Dan.
 
G

goosen_cug

I know the data is a garbage pointer.But the important thing is that
the funcation :Insert(int & x.int i) the & is vary important ,if
without & ,the data can not be passed to the data[MaxSize],and so can
not show the result on the screen.thank you !
"Dan Bloomquist дµÀ£º
"
goosen_cug said:
Although there are not problem,it can not run !

I didn't try to run it.

Just pasted and ran.
~SeqList ( ) { delete [ ] data; }

data is a garbage pointer. Didn't you see that? Otherwise, I'm sorry not
to fix your program...

Best, Dan.
 
B

Bernd Strieder

Hello,

goosen_cug said:
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

Use const int& in the parameter lists instead of int&. But here you
could use just plain int. Non-const references parameters may not be
bound to temporaries, as others in this thread have told, so non-const
reference parameters may cause restrictions, users of your code might
not want, so in general they are to be used only with special
consideration, e.g. you want to return some value through the
reference.

Do you know that your SeqList class is basically similar to
std::vector<int>, and that there are std::sort and std::set_union,
std::set_intersection, std::set_difference, which together provide
generally way better performing implementations than yours to the
problems you try to solve?

Bernd Strieder
 
D

Dan Bloomquist

goosen_cug said:
I know the data is a garbage pointer.But the important thing is that
the funcation :Insert(int & x.int i) the & is vary important ,if
without & ,the data can not be passed to the data[MaxSize],and so can
not show the result on the screen.thank you !

This is just not so. I see no member that requires a reference. The
'data(x)' is a value and can be push on the stack just fine with:
Insert(int x, int i);

Trace your program and the problem should become obvious.

Best, Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top