1
我試圖創建一個變量是一個節點指針的別名。g ++返回錯誤,當我嘗試使用指針的別名
到目前爲止,我曾嘗試以下語法:
node* _p = &p;
&
node* &_p = p;
每當我試圖調用一個類功能(即_p->的GetData())或甚至參考使用別名(如果(_p == NULL))我得到一個錯誤,指出:
error: expected primary-expression before ‘;’ token
node* newnode = _p;
我hav當我打電話時,e也嘗試在_p之前放置*或&,但它沒有幫助。
注:我用G ++
一個最小的,完整的,並且可驗證的例子(我認爲)編譯:列表類的插入函數內發生 的錯誤...
class node{
private:
int data;
node* next;
int mark;
public:
node(int d = 0, node* n = NULL){
data = d;
next = n;
mark = 0;
}
int getdata(){return data;}
node* getnext(){return next;}
int getmark(){return mark;}
void setdata(int x){data = x;}
void setnext(node* x){next = x;}
void setmark(int x){mark = x;}
};
class list{
private:
node* L1; //head pointers to list1, list2, and freelist.
node* L2;
node* free;
node* front;
public:
list(){
front = new node(); //generate free list (empty)
free = front;
L1 = NULL;
L2 = NULL;
node* p = free;
for (int i = 1; i < 10; i++){
p -> setnext(new node());
p = p -> getnext();
}
delete p;
}
void insert(int a, int x){
if (free == NULL) {
cout << a << " is full. can't insert " << x << " into the list." << endl;
return;
}
if (a == 1) node* &list = L1;
else if (a == 2) node* &list = L2;
else {
cout << "not a valid list";
return;
}
if (list == NULL){
list = free;
free = free -> getnext();
list -> setdata(x);
list -> setnext(NULL);
}
else {
node* p = list;
while (p -> getnext() != NULL) {p = p -> getnext();}
p -> setnext(free);
free = free -> getnext();
p -> getnext() -> setnext(NULL);
p -> getnext() -> setdata(x);
}
}
};
請提供產生此錯誤的[mcve]。 – chris
如果'p'是指向'node'的指針,請嘗試'node * _p = p;' – Knoep
node *&list = L1; list = list-> getnext(); 第二行是我的實際代碼chris的一個例子。 – ggkfox