473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
9 1557

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
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


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

"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

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


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
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: John Baker | last post by:
Hi: Some time ago I developed a program in Access, and separated the database and the program itself (using the normal access tools).We have the db on our server and the programin the desktop...
4
by: Darius | last post by:
How to distribute a C program into many C files ? this is the first question a C programmer asks after learning C basics, Questions like --what is a header file --where should i put global...
8
by: cdrsir | last post by:
like some useful windows commands, i.e., >>ping www.xxx.com if you only type >>ping it will give you just some options.
22
by: JoeC | last post by:
I am working on another game project and it is comming along. It is an improvment over a previous version I wrote. I am trying to write better programs and often wonder how to get better at...
11
by: Peted | last post by:
Im using c# 2005 express edition Ive pretty much finished an winforms application and i need to significantly improve the visual appeal of the interface. Im totaly stuck on this and cant seem...
23
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
3
by: Microsoft | last post by:
Hi I have a c# program that continually runs 24/7 and performs a variety of tasks based on a timer. There is one routine that hangs every Saturday morning without fail. If I restart the...
7
by: =?Utf-8?B?TWF4R3J1dmVu?= | last post by:
I have a DLL that implements the Business and Data Layers for a number of different websites. That DLL uses a Strongly Typed DataSet to interface to the Data Source which is SQL Server. There...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.