0
使用訪問函數,我試圖通過引用傳遞指針到另一個函數。使用訪問器函數在類中引用傳遞指針
該指針是班級跳過列表的私人成員,並指向a,yup,skip列表的頭部。
我需要通過引用將這個頭指針傳遞給插入函數,以便我可以在需要時更改頭指針指向的內容。
我可以看到,我的存取函數正在返回存儲在頭部的地址,而不是頭部本身的地址,但我不能爲我的生活計算如何解決這個問題。
我得到的錯誤是這樣的:
pointer.cpp: In function 'int main()':
pointer.cpp:32:29: error: no matching function for call to 'Skiplist::insert(Nod
e*)'
test.insert(test.get_head());
^
pointer.cpp:32:29: note: candidate is:
pointer.cpp:17:8: note: void Skiplist::insert(Node*&)
void insert(Node *&head);
^
pointer.cpp:17:8: note: no known conversion for argument 1 from 'Node*' to 'No
de*&'
下面是代碼的一個非常精簡的版本:
#include <iostream>
using namespace std;
class Node
{
public:
private:
};
class Skiplist
{
public:
void insert(Node *&head);
Node *get_head() const;
private:
int level_count;
Node *head;
};
int main()
{
Skiplist test;
test.insert(test.get_head());
return 0;
}
Node *Skiplist::get_head() const
{
return head;
}
void Skiplist::insert(Node *&head)
{
//bla bla bla
}
'get_head'返回一個指針,而不是對指針的引用。 – Barmar
'get_head()'返回值,這將是一個臨時的,不能綁定到非常量的左值引用。 – songyuanyao