2011-03-11 60 views
2

我收到「下一個」和「上一個」變量的不完整類型錯誤。我不確定自己做錯了什麼,因爲我在C++編寫類時非常生疏。任何幫助,將不勝感激! 謝謝。未完成類型

#include<iostream> 

using namespace std; 

class LinearNode 
{ 
    public: 
     //Constructor for the LinearNode class that takes no arguments 
     LinearNode(); 
     //Constructor for the LinearNode class that takes the element as an argument 
     LinearNode(int el); 
     //returns the next node in the set. 
     LinearNode getNext(); 
     //returns the previous node in the set 
     LinearNode getPrevious(); 
     //sets the next element in the set 
     void setNext(LinearNode node); 
     //sets the previous element in the set 
     void setPrevious(LinearNode node); 
     //sets the element of the node 
     void setElement(int el); 
     //gets the element of the node 
     int getElement(); 

    private: 
     LinearNode next; 
     LinearNode previous; 
     int element;   
};//ends the LinearNode class 

實現文件:

#include<iostream> 
#include"LinearNode.h" 

using namespace std; 

//Constructor for LinearNode, sets next and element to initialized states 
LinearNode::LinearNode() 
{ 
    next = NULL; 
    element = 0; 
}//ends LinearNode default constructor 

//Constructor for LinearNode takes an element as argument. 
LinearNode::LinearNode(int el) 
{ 
    next = NULL; 
    previous = NULL; 
    element = 0; 
}//ends LinearNode constructor 

//returns the next element in the structure 
LinearNode::getNext() 
{ 
    return next; 
}//ends getNext function 

//returns previous element in structure 
LinearNode::getPrevious() 
{ 
    return previous; 
}//ends getPrevious function 

//sets the next variable for the node 
LinearNode::setNext(LinearNode node) 
{ 
    next = node 
}//ends the setNext function 

//sets previous for the node 
LinearNode::setPrevious(LinearNode node) 
{ 
    previous = node; 
}//ends the setPrevious function 

//returns element of the node 
LinearNode::getElement() 
{ 
    return element; 
}//ends the getelement function 

//sets the element of the node 
LinearNode::setElement(int el) 
{ 
    element = el; 
}//ends the setElement function 

測試文件:

#include<iostream> 
#include"LinearNode.h" 

using namespace std; 

int main() 
{ 
    LinearNode node1, node2, move; 
    node1.setElement(1); 
    node2.setElement(2); 

    node2.setNext(node1); 
    node1.setPrevious(node2); 

    move = node2; 

    while(move.getNext() != NULL) 
     cout << move.getElement() << endl; 

} 
+1

請發佈整個錯誤消息。這非常有幫助。 – James 2011-03-11 17:22:03

回答

4

你需要指定返回類型爲您的所有的.cpp功能。 例如:

//returns previous element in structure 
LinearNode LinearNode::getPrevious() 
{ 
    return previous; 
}//ends getPrevious function 
+1

斑點,這是一個額外的問題。 – 2011-03-11 17:27:24

0

你的類型LinearNode的成員的情況下,他們不允許在這方面。請記住,在C++中,如果聲明「Foo f」,那麼不是像在C#中那樣引用堆對象,而是在堆棧或類佈局中存在Foo的實例。

更糟糕的是,那些兩個LinearNode實例在實例化它們包含的實例時將實例化爲。它們每個都有兩個子實例,它們將同樣被實例化,並且每個實例都有兩個......等等,直到你用完內存。當然你不能這麼做,因爲這沒有意義。

所以那些必須是指針或引用。原因在於編譯器必須知道這兩個實例的大小才能確定LinearNode類的大小,但在知道大小之前必須知道該大小。

14

你的類型有一個遞歸定義,這是禁止的。

class LinearNode 
{ 
private: 
    LinearNode next; 
    LinearNode previous; 
}; 

的數據成員和nextpreviousLinearNode類,它尚未完全定義的實例(未引用或指針)。

你可能想這樣的:

class LinearNode 
{ 
private: 
    LinearNode * next; 
    LinearNode * previous; 
}; 
+0

爲學習而歡呼 – 2011-03-11 17:29:34

0

這樣看來,你試圖impliment一個「鏈接列表」

的結構鏈接的是,接下來的&以前的「點」(*)和實際上並不像Andre說的那樣是另一個節點。

http://www.cplusplus.com/doc/tutorial/pointers/可能有助於澄清。

class LinearNode 
{ 
//...snip 
//returns the next node in the set. 
LinearNode* getNext(); 
//returns the previous node in the set 
LinearNode* getPrevious(); 

//...snip 
private: 
    LinearNode * next; 
    LinearNode * previous; 
}; 

所以在實現文件:

//returns the next element in the structure 
LinearNode* LinearNode::getNext() 
{ 
    return next; // now a poiner 
}//ends getNext function 

//returns previous element in structure 
LinearNode* LinearNode::getPrevious() 
{ 
    return previous; // now a poiner 
}//ends getPrevious function 

//sets the next variable for the node 
void LinearNode::setNext(LinearNode* node) //pointer in, void out 
{ 
    next = node 
}//ends the setNext function 

//sets previous for the node 
void LinearNode::setPrevious(LinearNode* node)//pointer in, void out 
{ 
    previous = node; 
}//ends the setPrevious function 

所以主要的樣子:

int main() 
{ 
    LinearNode node1, node2, move; 
    node1.setElement(1); 
    node2.setElement(2); 

    node2.setNext(&node1); // give the address of node to the next pointer (&) 
    node1.setPrevious(&node2); // give the address of node to the next pointer (&) 

    move = node2; 

    while(move.getNext() != NULL) 
     cout << move.getElement() << endl; 

} 

我不知道是什麼,while循環的嘗試,雖然這樣做!如果 - 也許?

同樣Nicklamort聲明你的所有類函數都需要返回類型,除非是void。

+0

我不是故意粗魯或任何事情,但你只是在這裏重新措辭了2個其他答案。此外,您的帖子似乎有一個斷開的鏈接(未定義它,也許?)。 – 2011-03-11 19:06:18

+0

一點也不粗魯,我只是試圖把所有的東西放在一起,用於固定鏈接 – Brandrew 2011-03-14 18:44:27