views:

142

answers:

2

I just started learning C++ (coming from Java) and am having some serious problems with doing anything :P Currently, i am attempting to make a linked list, but must be doing something stupid cause i keep getting "void value not ignored as it ought to be" compile errors (i have it marked where it is throwing it bellow). If anyone could help me with what im doing wrong, i would be very grateful :)

Also, I am not used to having the choice of passing by reference, address, or value, and memory management in general (currently i have all my nodes and the data declared on the heap). If anyone has any general advice for me, i also wouldn't complain :P

Key code from LinkedListNode.cpp

LinkedListNode::LinkedListNode()
{
    //set next and prev to null
    pData=0; //data needs to be a pointer so we can set it to null for
             //for the tail and head.
    pNext=0;
    pPrev=0;
}

/*
 * Sets the 'next' pointer to the memory address of the inputed reference.
 */
void LinkedListNode::SetNext(LinkedListNode& _next)
{
    pNext=&_next;
}

/*
 * Sets the 'prev' pointer to the memory address of the inputed reference.
 */
void LinkedListNode::SetPrev(LinkedListNode& _prev)
{
    pPrev=&_prev;
}
//rest of class

Key code from LinkedList.cpp

#include "LinkedList.h"

LinkedList::LinkedList()
{
    // Set head and tail of linked list.
    pHead = new LinkedListNode();
    pTail = new LinkedListNode();

     /*
      * THIS IS WHERE THE ERRORS ARE.
      */
    *pHead->SetNext(*pTail);
    *pTail->SetPrev(*pHead);
}
//rest of class
+6  A: 

The leading * in

*pHead->SetNext(*pTail);
*pTail->SetPrev(*pHead);

are not needed.

pHead is a pointer to a node and you call the SetNext method on it as pHead->SetNext(..) passing an object by reference.

-> has higher precedence than *

So effectively you are trying to dereference the return value of the function SetNext which does not return anything, leading to this error.

codaddict
Thanks, worked like a-charm. Thanks for the explination too, that makes much more sense.
kyeana
Right. The error comes from trying to use the `*` (dereference operator) on the return value from `pHead->SetNext(*pTail)`. Member-access and function-call both take precedence over dereference.
Ben Voigt
+4  A: 

Also, I am not used to having the choice of passing by reference, address, or value, and memory management in general (currently i have all my nodes and the data declared on the heap). If anyone has any general advice for me, i also wouldn't complain :P

Ex-Java programmers always do that. And it's upside down. You should virtually never heap-allocate data. Objects should be declared on the stack, and if they need heap-allocated memory, they should handle that internally, by allocating it in their constructors and releasing it in their destructors.

That leads to cleaner and safer code.

Class members should also be values, not pointers/references unless you specifically need the member to be shared between different objects. If the class owns its member exclusively, just make it a non-pointer value type. That way it's allocate inside the class itself, and you don't need to keep track of new/delete calls.

The simplest rule of thumb is really to not use pointers unless you have to. Do you need the object to be allocated elsewhere? Why can't it be allocated here and be accessed by value? Even if the object has to be returned from a function, or passed as parameter to another function, copying will usually take care of that. Just define appropriate copy constructors and assignment operators and copy the object when necessary.

jalf