アクセサ関数を使用して、別の関数への参照によってポインタを渡そうとしています。クラス内のアクセサ関数を使用して参照によるポインタを渡す
このポインタはSkiplistクラスのプライベートメンバーであり、a、yupスキップリストの先頭を指しています。
私は必要に応じてヘッドポインタが何を指しているかを変更できるように、このヘッドポインタを挿入関数に渡す必要があります。
私のアクセサー機能は、ヘッド自体のアドレスではなく、ヘッドに格納されたアドレスを返すことがわかりますが、私の人生にとってはこれを解決する方法を理解できません。
私が手にエラーがこれである:ここでは
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()'は値によって返します。これは一時的なものであり、非constへの左辺参照にバインドすることはできません。 – songyuanyao