template<typename Type> class ListNode{
private:
Type nodeData;
ListNode *nextNode;
friend class SingleList<Type>;
ListNode():nextNode(NULL){}
ListNode(const Type item,ListNode<Type> *next=NULL):nodeData(item),nextNode(next){}
~ListNode(){
nextNode=NULL;
}
public:
Type GetData();
friend ostream& operator<< <Type>(ostream& ,ListNode<Type>&);
};
template<typename Type> Type ListNode<Type>::GetData(){
return this->nodeData;
}
//here, compiler said 'nodeData is a private member of ListNode<int>'
編譯器說,「NODEDATA是的私有成員ListNode」如何讓C++中的私有變量
我怎樣才能得到一個私有變量?
或c + +中的任何get/set函數,如Java?
「這裏」是指在GetData函數處,返回this-> nodeData;或者在右大括號之後的空白處? – doctorlove
@doctorlove在這裏意味着GetData函數,返回this-> nodeData – tonysok
如果您將模板中的GetData定義移動了,該怎麼辦?即Type GetData(){return nodeData; }; –