By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,370 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i];
}
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[i] != 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[i] = 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[i]<<",";
}
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],i);
B.Insert(Array2[i],i);
}
Intersection(A,B);
A.print();

}
How to improve this problem?
Thank you !

Nov 10 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

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 wrote:
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[i];
}
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[i] != 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[i] = 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[i]<<",";
}
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],i);
B.Insert(Array2[i],i);
}
Intersection(A,B);
A.print();

}
How to improve this problem?
Thank you !
Nov 10 '06 #2

P: n/a
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.
Nind...@yahoo.co.uk wrote:
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 wrote:
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[i];
}
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[i] != 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[i] = 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[i]<<",";
}
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],i);
B.Insert(Array2[i],i);
}
Intersection(A,B);
A.print();

}
How to improve this problem?
Thank you !
Nov 10 '06 #3

P: n/a


goosen_cug wrote:
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.

Nov 10 '06 #4

P: n/a

"Dan Bloomquist д
"
goosen_cug wrote:
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.
Although there are not problem,it can not run ! thank you

Nov 10 '06 #5

P: n/a

goosen_cug wrote in message
<11**********************@f16g2000cwb.googlegroups .com>...
>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.

--
Bob R
POVrookie
Nov 10 '06 #6

P: n/a


goosen_cug wrote:
>
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.

Nov 10 '06 #7

P: n/a
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 wrote:

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.
Nov 10 '06 #8

P: n/a
Hello,

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

Nov 10 '06 #9

P: n/a


goosen_cug wrote:
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.

Nov 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.