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 below). If anyone could help me with what I'm 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
eddie
  • 1,252
  • 3
  • 15
  • 20
vimalloc
  • 3,869
  • 4
  • 32
  • 45
  • http://warp.povusers.org/grrr/java2.html – Nicolás Apr 17 '10 at 05:13
  • I strongly discourage taking the address of a reference. Instead just pass the pointer. This way the caller knows that you're saving the address and is less likely to pass in a local variable. – Ben Voigt Apr 17 '10 at 05:15
  • @Ben: bear with me, im trying to figure this all out. So i am assuming you are talking about the SetNext and SetPrev functions? The end user should not have access to them, they should only have access to, say, the add and remove methods, which will then call them. That being said, would it still make more sense to pass by address in this case? My original though for pass by function, was that by using const, we wouldn't be able to change the data there, only use it (in this case get the address). However, my mind is turning every which way trying to understand all of this, so help is welcome! – vimalloc Apr 17 '10 at 05:33
  • Yes, I'm precisely talking about SetNext and SetPrev. I would never keep a parameter passed by reference longer than the duration of the called function. The caller might not even know a reference is being taken. For private functions what you've done may be "safe" but it's not good practice and hurts maintainability. – Ben Voigt Apr 17 '10 at 05:45
  • Please pick up a beginner's C++ book (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). To learn C++ properly, pretend you never learned Java. C++ is nothing like Java. – In silico Sep 13 '10 at 14:31

2 Answers2

6

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
  • 445,704
  • 82
  • 492
  • 529
  • Thanks, worked like a-charm. Thanks for the explination too, that makes much more sense. – vimalloc Apr 17 '10 at 05:14
  • 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 Apr 17 '10 at 05:14
3

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
  • 243,077
  • 51
  • 345
  • 550