Copy Function changing value?

This is a discussion on Copy Function changing value? within the C++ Programming forums, part of the General Programming Boards category; Ok the result of my copy function is weird. For people going to look at it. I am using an ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    7

    Copy Function changing value?

    Ok the result of my copy function is weird. For people going to look at it. I am using an array of records where everything stored is in the same array. So if i put in 3 and 2 as one set then 4 and 7 as one set has

    location: 0 | .info 3 | .next NUL
    location: 1 | .info 2 | .next 0
    location: 2 | .info 4 | .next NUL
    location: 3 | .info 7 | .next 2

    If i were to print out the original list BEFORE i exit the copy function it prints it fine and everything is correct.

    EDITED FROM ORIGINAL POST:
    It is also weird that I pass the original list in as a constant so it SHOULD not be allowed to changed
    END EDITED FROM ORIGINAL POST:

    However if I print it in my driver it prints ONLY the first term wrong

    Exampe:
    Enter a polynomial function in the form of: "2x^2 + 3x^2" ==> 4x^5 - 3 + 3x^5

    The original fucntion in copy function: 4x^5 - 3 + 3x^5
    (now in driver
    The original function is: 1075464688 - 3 + 3x^5
    The term with the highest highest degree is: 4x5
    and the degree is: 5
    The derivative is: 20x^40 + 15x^4

    I have no clue why this is happening. I traced my program, when I print the location where the index is printing in the driver the index is correct and is 0. So it is not pointing to the wrong index. FOr some reason my .info changes when I try and print it inside my driver but in my copy function it is not changed.

    I know this question may be hard for you guys to answer but if I can recieve ANY help i be grateful.

    GetIndex() returns the location of the first item (basically where NUL is
    and
    GetListData(): Returns the location of the last item
    so location 1 for the original list and location 2 for the new

    GetNode(location2, storage);
    finds the next free node in storage and puts it value into location2

    Code:
    void Polynomial::operator=(const Polynomial old)
    {
      int location = old.GetIndex();
      int location2 = 999; // weird
      Terms toCopy;
    
      for (int i = location; i <= old.GetListData(); i++)
      {
        // makes a node, index=locatio2
        GetNode(location2, storage);
    
        if (i == old.GetIndex())
          SetIndex(location2);
    
        toCopy = storage.nodes[i].info;
        storage.nodes[location2].info = toCopy;
        storage.nodes[location2].next = listData;
        listData = location2;
      }
    }
    Last edited by GMHummerH1; 12-20-2004 at 12:17 AM.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    My guess is Terms does not copy right. May I see code for Terms?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    My experience has been that when I get weird output like that I have forgotten to initialize/assign a value to a variable. Seeing more code, with appropriate comments, would help those willing to help you try to track it down.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21