please help!! assignment operator

B

Brett Irving

Hi could someone please help me on this cause I keep having problems
with it
I want to be able to assign one object to another but everytime I do
so it keeps segfaulting on me, Ive tried about 10 different ways of
trying to get this right and it just wont work

here is my code

I have globals char* name and int bikeReg


cyclist& cyclist::eek:perator=(const cyclist& cyc)
{
if( this != &cyc )
{
delete[] name;

name = new char[strlen(cyc.name)+1]; // get new space

strcpy(name, cyc.name); // copy new name

bikeReg = cyc.bikeReg;

}
return *this;
}

Thanks a lot
 
D

David White

Brett Irving said:
Hi could someone please help me on this cause I keep having problems
with it
I want to be able to assign one object to another but everytime I do
so it keeps segfaulting on me, Ive tried about 10 different ways of
trying to get this right and it just wont work

here is my code

I have globals char* name and int bikeReg

Members, I suggest, not globals.
cyclist& cyclist::eek:perator=(const cyclist& cyc)
{
if( this != &cyc )
{
delete[] name;

I can see nothing wrong here. Is it possible that you have not initialized
'name' in your constructor? If you haven't, then deleting it is likely to
crash the program.
name = new char[strlen(cyc.name)+1]; // get new space

strcpy(name, cyc.name); // copy new name

bikeReg = cyc.bikeReg;

}
return *this;
}

David
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top