473,670 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

This program is a "Sequential List" class I want to do the Union
Operation,Inter section Operation of the
Set.But this program have a problem:
///////////////////////////
Compiling...
Set.cpp
H:\cheung\Set\S et.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<iostre am>
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(SeqL ist & 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 1579

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,Inter section Operation of the
Set.But this program have a problem:
///////////////////////////
Compiling...
Set.cpp
H:\cheung\Set\S et.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<iostre am>
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(SeqL ist & 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.c o.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,Inter section Operation of the
Set.But this program have a problem:
///////////////////////////
Compiling...
Set.cpp
H:\cheung\Set\S et.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<iostre am>
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(SeqL ist & 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\S et.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\S et.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************ **********@f16g 2000cwb.googleg roups.com>...
>This program is a "Sequential List" class I want to do the Union
Operation,Inte rsection 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<iostre am>
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,Inter section Operation of the
Set.But this program have a problem:
///////////////////////////
Compiling...
Set.cpp
H:\cheung\Set\S et.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_inters ection, std::set_differ ence, 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
5070
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 system. I have modified the program a number of times, and now find that I need to change the DB slightlt. This appears to require that I REMERGE the data base and program, and I have no idea how to do that. Can someone give me some pointers,...
4
1736
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 variables --where should i put declaration and definitions --where should i put macros --when to use static functions and variables
8
1756
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
2706
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 programming. I tend to learn what is useful and gets the job done. I am always curious if there is some techique I don't know. I read books and study as well as write programs. My goal is to some day be able to get a job programming. I have a...
11
3375
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 to work out how to start on a solution. I have of course used a varienty of componets, mostly radio buttons with "button" appearence.
23
2253
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 crippled all the constructors. So now all the operations and constructors that were used do is just return BigFloats but no memory is actually accessed at any point, nor is any allocated. However, when I reenable those parts of the constructor that...
41
2672
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 will calculate the factorial of 10, 10 millions time and print the reusult in a file with the name log.txt.. I wrote something like this
3
1682
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 program throughout the week is still hangs. I can restart it immediatly after the hang and it runs fine until next Saturday. There is nothing different about Saturday that I can think of.
7
1736
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 is a connection string in the DLL that is causes me grief. It points at a particular instance of database. I hae to recompile the DLL for each WebSite. How can I re-use that DLL on different websites and have it point to different instances of...
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8388
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8593
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8663
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7423
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.