0

So I am new to c++ sorry if this is not to clear.

I have a class:

class Item
{
    int noItem;
    int qItem;
public:
    Item(int noItem, int qItem)
    {
            this->noItem = noItem;
            this->qItem = qItem;
    }

    int getNoItem()
    {
            return noItem;
    }

    int getQntItem()
    {
            return qItem;
    }
};

Then the following class:

class Element
{
public:
    Element()
    {
            data = NULL;
    }

    //to set and access data hold in node
    void setElement(Item *data)
    {
            this->data = data;
    }
    Item* getElement(void)
    {
            return(data);
    }

private:
    Item *data;
};

This one also:

class ListeChainee
{
public:

    ListeChainee()
    {
        courant = NULL;
    }
    void ajoutListe(Item *data)
    {
        Element *newData;

        //set data
        newData->setElement(data);

        //check if list is empty
        if( courant == NULL)
        {           
            //set current pointer
            courant = newData;
        }


    }

    //get data from element pointed at by current pointer
    Item* elementCourant(void)
    {
            if(courant != NULL)
            {
                return courant->getElement();
            }
            else
            {
                return NULL;
            }
    }

private:
    //data members
    Element *courant;           //pointer to current element in list

};

The code is missing some stuff for other things, but my problem is this:

int main(int argc, char* argv[])
{
    ListeChainee listeCH;
    Item i1(123,456);

    listeCH.ajoutListe(&i1);

    cout << listeCH.elementCourant()->getNoItem();

    system("pause");

    return 0;
}

I expect 123 to be outputted, but I see some other number. Not sure why. Thanks.

3
  • In a linked list, you have a node structure or class that usually includes a "next" pointer that points to the "next" node in the list, and/or optionally, a "previous" pointer that points to the previous node. The "list" itself, will include a pointer to the first and/or last node in the list. Commented Oct 6, 2014 at 0:41
  • @imreal I replaced the real variable name here, sorry should have chosen something else beside new.. Commented Oct 6, 2014 at 0:43
  • @rcgldr yes I do have all of those, I just removed them here for the sake of clarity. Commented Oct 6, 2014 at 0:44

2 Answers 2

2

Your Element *newData doesn't have an instance of Element class, so it will crash when you try to access the instance pointed by newData.

Try to change Element *newData; to Element *newData = new Element;.

  • ps.: Don't forget to delete it when you don't need the instance any more.
Sign up to request clarification or add additional context in comments.

1 Comment

That was exactly it, I just missed it. Thanks!
1

This method is writing to uninitialized memory:

void ajoutListe(Item *data)
{
    Element *new;

    //set data
    new->setElement(data); // Right here, "new" is an uninitialized pointer

    //check if list is empty
    if( courant == NULL)
    {           
        //set current pointer
        courant = new;
    }
}

I'm surprised this compiles (does it?). This code should also crash.

The strange number you're getting is surely some random part of memory. You may want to think more about memory management -- there are numerous problems here. When ajoutListe is called, why does the courant member only get set if it's NULL? Do we just leak the new Element? How do we actually traverse this list?

1 Comment

Thanks you are right, I needed an instance of it. And about the NULL thing there's actually an else statement I just removed it before posting here since it was not really needed. I removed all the list operations, etc.. My code is more complete. I works with an instance, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.