2010-11-01 176 views
4

我使用Borland 5.5編譯我的代碼,並且沒有彈出錯誤。但它沒有正確運行,所以我決定使用Visual Studio 2010來調試我的程序。C++ - 操作符重載錯誤C4430:缺少類型說明符 - int假定

Visual Studio是給我這個錯誤:

Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\johnny\documents\visual studio 2010\projects\stack_linkedlist\stack_linkedlist\classstack.cpp 111 1 STACK_LinkedList 

它是指向我的運算符重載函數。這是我的操作符重載的代碼。

//operator overload 
template <class S> 
const Stack<S>::operator=(const Stack& s) 
{ 
    // Check for self assignment 
    if (&s==this) 
     return *this; 

    // Clear the current stack 
    while (s.theFront) 
     { 
      NodePointer p = s.theFront; 

      s.theFront = s.theFront->next; 
      delete p; 
     } 

     s.theTop = s.theFront; 


    // Copy all data from stack s 
    if (!s.isEmpty()) 
    { 
     NodePointer temp = q->theFront; 

     while(temp != 0) 
     { 
      push(temp->data); 
      temp = temp->next; 
     } 
    } 

    return *this; 
} 

任何幫助都會很棒!謝謝!

+2

嗯...好像編譯器試圖告訴你一些事情......它可能是什麼? – 2010-11-01 21:49:21

回答

9

沒有爲您的操作員定義返回類型。

const Stack<S>::operator=(const Stack& s) 

應改爲:

const Stack<S>& Stack<S>::operator=(const Stack& s) 
3

您的方法缺少返回類型。試試這個:

template <class S> 
const Stack<S>& Stack<S>::operator=(const Stack& s) 
{ 
    // body of method 
} 
1
template <class S>const Stack<S>::operator=(const Stack& s) 

在這個函數的聲明,你缺少返回類型。

如果你想分配堆棧對象,試試這個 -

template <class S> 
Stack<S>& Stack<S>::operator=(const Stack& s) 

重載賦值運算符必須做兩件事情 -

  1. 執行任務。
  2. 返回*這。這將隱含地支持形式a = b = c的多個asisgnments。

既然你回來*此,在函數聲明中指定的返回類型相匹配的類型的*這。在這種情況下,這將是Stack<S>&

+0

只需修正您的最終陳述,使其成爲參考並添加代碼標籤以顯示模板參數。 – Puppy 2010-11-01 21:56:29

+0

因爲*的類型不是Stack &,所以我故意將它作爲堆棧(與堆棧&)對齊。返回類型被指定爲Stack 的原因可能超出了本討論的範圍,但我認爲最好讓* this的類型被稱爲Stack 而不是作爲參考Stack ? – Vatsan 2010-11-01 22:00:22

相關問題