please help!! assignment operator

Discussion in 'C++' started by Brett Irving, Jun 29, 2003.

  1. Brett Irving

    Brett Irving Guest

    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
     
    Brett Irving, Jun 29, 2003
    #1
    1. Advertising

  2. Brett Irving

    David White Guest

    "Brett Irving" <> wrote in message
    news:...
    > 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
     
    David White, Jun 29, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    596
    Big Brian
    Oct 14, 2003
  2. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    492
    Barry Schwarz
    Feb 8, 2005
  3. Chris
    Replies:
    34
    Views:
    1,528
  4. Daniel Fetchinson
    Replies:
    4
    Views:
    436
    Stephen Johnson
    Sep 11, 2008
  5. Hicham Mouline
    Replies:
    2
    Views:
    697
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page