2011-11-04 51 views
3

我想我的程序組織到功能,並遇到了這個,缺少'。'之前的模板參數。令牌

error: "missing template arguments before '.' token"

一次我嘗試運行功能的代碼,它工作正常,如果它只是在main()。任何熟悉這個錯誤的人都知道這個問題可能是什麼?

注意,註釋掉的代碼刪除錯誤,但與有序列表class混亂和重置其長度或東西,引起orderedlist.getlength()功能return 0,這使得沒有代碼在while()循環中執行。

功能:在orderedList類(其中長度被確定)

void rentFilm(char* filmId, char* custId, char* rentDate, char* dueDate, int numFilm) 
{ 
    //orderedList <filmType> orderedList(numFilm); 
    //filmType newItem; 
    int index = 0; 
    bool found = false; 

    while (index < orderedList.getLength() && !found) 
     { 
      cout << "test" << endl; 
     if (strncmp(filmId,orderedList.getAt(index).number,6) == 0 && strncmp("0000",orderedList.getAt(index).rent_id,5) == 0)//If that film is rented by NO customer 
      { 
       cout << "test" << endl; 
       found = true;//customer can rent it 
       strcpy(newItem.number,filmId); 
       orderedList.retrieve(newItem); 
       orderedList.remove(newItem); 
       strcpy(newItem.rent_id,custId); 
       strcpy(newItem.rent_date,rentDate); 
       strcpy(newItem.return_date,dueDate); 
       orderedList.insert(newItem); 
       cout << "Rent confirmed!" << endl; 
      } 
     else 
      { 
       if (strncmp(filmId,orderedList.getAt(index).number,6) > 0 || strncmp("0000",orderedList.getAt(index).rent_id,5) > 0) 
        { 
         ++ index; 
        } 
       else 
        { 
        throw string ("Not in list"); 
        } 
      } 
     } 
} 

插入:在主

template <class elemType> 
void orderedList<elemType>::insert(const elemType& newItem) 
{ 
    int index = length - 1; 
    bool found = false; 

    if (length == MAX_LIST) 
     throw string ("List full - no insertion"); 

     // index of rear is current value of length 

    while (! found && index >= 0) 
     if (newItem < list[index]) 
     { 
      list[index + 1] = list [index]; // move item down 
      --index; 
     } 
     else 
      found = true; 

    list [index + 1] = newItem; // insert new item 
    ++length; 
} 

代碼,其中列表被填充:

filmFile.open("films.txt", ios::in); 
filmFile >> numFilm; 
filmFile.get(); 

orderedList <filmType> orderedList(numFilm); 
filmType newItem; 

readString(filmFile, newItem.number,5); 
    for (int i = 0; i < numFilm; i++) 
    { 
     newItem.copy = filmFile.get(); 
    readString(filmFile, newItem.title,30); 
     readString(filmFile, newItem.rent_id,4); 
     readString(filmFile, newItem.rent_date,8); 
     readString(filmFile, newItem.return_date,8); 
     filmFile.get(); 

     orderedList.insert (newItem);//puts filmType struct into the ordered list. 

     readString(filmFile, newItem.number,5); 
    } 

請讓我知道如果程序中的其他地方的代碼將有助於評估此錯誤。

+0

什麼是'orderedlist'?它是'班'嗎? – iammilind

+0

是的,它是一個類,而orderedList(numFilm)是一個構造函數 – darko

+0

這段代碼似乎沒有任何問題。您可能希望爲'class orderlist'提供最少的**代碼。 – iammilind

回答

1

它看起來像你註釋掉的行聲明一個變量與一個類的名字相同。

所以當你註釋掉它時,那個類的靜態函數會被調用。

更改聲明是這樣的:

orderedList<filmType> filmList(numFilm); 

,然後更改的orderedList所有引用的函數filmList

+0

這與將列表長度重置爲0的效果相同,就像'orderedList'未重命名爲其他內容一樣。 – darko

+0

我的猜測是你需要將所有元素實際插入到列表中以使其長度不爲零。我對你正在實例化的類沒有任何瞭解,但我敢打賭,你傳遞給構造函數的參數只是提示要在一個向量中預先分配多少元素,所以不需要增加向量。 –

+0

對,這是如何,長度是確定的。隨着項目被插入課程中,長度會增加。 – darko

0

問題是您正在創建一個與模板名稱相同的變量?當你說,

orderedList<filmType> orderedList(numFilm); 

這是(在某種程度上)好像是說,

int int=42; 

,然後期待int+1返回43

試着這麼做,

orderedList<filmType> ol(numFilm); 

而且起了變化所有其他提到orderedList,到ol

+0

這與moishes建議類似,它與我的數據混淆程序。我使用'orderedList orderedList(numFilm);'在main中,它似乎工作正常 – darko

+0

是否知道'orderedList(numFilm);'是我的orderedLists構造函數在這裏有什麼意義? – darko

+0

@mwmnj:不,它是從該構造函數創建的變量。 –

0

看起來您在main()中填充變量orderedList,然後在您聲明具有相同名稱時希望它在rentFilm(...)中自動可用;這是不可能的。你必須通過對象從main()或更好的功能,使該功能爲class orderedList的成員方法:

int main() 
{ 
    orderedList<filmType> ol(numFilm); // variable name different (good practice) 
    ... // all the populating 
    orderedList.rentFilm(...); // call the function like this 
} 

其中,rentFilem()現在的class

class orderedList { 
... 
public: 
    void rentFilm(char* filmId, char* custId, char* rentDate, char* dueDate, int numFilm); 
}; 

部分現在裏面的函數,您不必爲orderedList聲明變量;只需使用this-><method/variable>即可。它應該工作。

+0

您需要'''左側的對象名稱,而不是類模板。這就是問題所在。 –

+0

@BenVoigt,如果OP評論代碼,則會出現該問題。在問題的後面部分,OP會問,'爲什麼如果代碼沒有註釋,它不會工作'? (實際上模板類名稱和編譯器允許的對象名稱相同) – iammilind

+0

是的,但我正在談論您的「固定」代碼,它有相同的問題。 「這樣調用函數」的評論確實不是。 –