Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning C++ from Java , trying to make a linked list

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
like image 782
vimalloc Avatar asked Nov 24 '25 14:11

vimalloc


1 Answers

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.

like image 193
codaddict Avatar answered Nov 26 '25 03:11

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!