vim*_*loc 2 c++ java pointers linked-list parameter-passing
我刚刚开始学习C++(来自Java)并且在做任何事情时遇到了一些严重的问题:P目前,我正在尝试制作一个链表,但是必须做一些愚蠢的事情,因为我一直得到"空值不被忽略,因为它应该是"编译错误(我把它标记在下面扔它的地方).如果有人能帮我解决我做错了什么,我将非常感激:)
此外,我不习惯选择通过引用,地址或值传递,以及一般的内存管理(目前我的所有节点和数据都在堆上声明).如果有人对我有任何一般性建议,我也不会抱怨:P
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
领先*的
*pHead->SetNext(*pTail);
*pTail->SetPrev(*pHead);
Run Code Online (Sandbox Code Playgroud)
不需要.
pHead是一个指向节点的指针,您可以SetNext在其上调用方法作为pHead->SetNext(..)传递object引用.
->具有更高的优先级比*
因此,您有效地尝试取消引用SetNext不返回任何内容的函数的返回值,从而导致此错误.