2012-03-17 20 views
0

函數定義C++對操作者的gcc誤差=函數

const circularLinkedList<Tp>& operator=(const circularLinkedList<Tp>& otherList); 

線導致錯誤,錯誤消息指線路327,其開始於節點類型....

template <class Tp> 
nodeType<Tp>* circularLinkedList<Tp>&::operator=(const circularLinkedList<Tp>& otherList) 

和誤差從gcc編譯器消息:

circularLinkedList.h:327: error: invalid declarator before â&â token 
circularLinkedList.h:327: error: expected initializer before â&â token 

我認爲我做了某種語法錯誤在定義這個方法somewh ERE。我將如何去修復它?謝謝。

+0

是一個'A&A'令牌只是一個'&'令牌奇怪的編碼報價? – 2012-03-17 18:53:29

+0

@MrLister - 我認爲是。也許複製/粘貼問題。 – 2012-03-17 18:55:34

+2

是不是&前::運營商錯了? – abresas 2012-03-17 18:57:08

回答

2

你能爲我們多發一點代碼嗎?你能解釋一下nodeType是什麼嗎?

下看起來像一個函數定義:

template <class Tp> 
nodeType<Tp>* circularLinkedList<Tp>&::operator=(const circularLinkedList<Tp>& otherList) 

然而,對於一兩件事,聲明說,它返回一個const circularLinkedList<Tp>&。 另外,您不希望在::之前有&。它需要是類型的名稱,而不是指向該類型變量的指針或引用。如果你想要這種行爲,你需要使用代理類。

因此,它應該是這樣的:

template <class Tp> 
const circularLinkedList<Tp>& circularLinkedList<Tp>::operator=(const circularLinkedList<Tp>& other) 

哪些應該幾乎無一例外return *this;

+0

這是一個函數定義,並確實返回* this。但你實際上修正了這一行。我不確定如何獲得正確的函數定義,並且無法使其工作,直到您回答。謝謝! – rfmas3 2012-03-17 18:58:43

0

最終在第一個代碼塊,你證明了該方法的聲明,而不是函數定義。 在第二個代碼塊中,顯示方法定義的標題。

方法聲明: 返回const circularLinkedList<Tp>&

方法定義: 返回nodeType<Tp>*

您沒有定義您聲明的方法。 您正在定義您尚未聲明的某種方法。

定義的標題應該是:

const circularLinkedList<Tp>& circularLinkedList<Tp>::operator=(const circularLinkedList<Tp>& otherList)