2016-08-01 23 views
0

我試圖使用模板,並且我總是使用模板進行noob。一旦我開始,我會收到一些錯誤,我不明白這是什麼意思? 所以文本是在這裏,我要做的事情:模板 - 無法將函數定義與現有聲明相匹配C++

編寫一個基於模板的類來實現一組項目。該類應該允許 用戶到 a。將新項目添加到該集合。 b。獲取集合中的項目數量。 c。獲取一個指向包含集合中每個項目的動態創建的數組的指針。這個函數的調用者負責解除分配內存。

的錯誤是:

項目::輸出「:無法定義的功能匹配到一個現有的聲明

項目::在」:無法定義的功能匹配到一個現有的聲明

添加「:是不是成員」項

我的代碼是在這裏:

#include <iostream> 
using namespace std; 

template<class T> 
class Item { 
private: 
    Item(); 
    ~Item(); 
    void Add(T item); 
    int get(); 
    void output(T array); 
    bool in(T item); 
    T *array; 
    int element; 
    int size; 
}; 

template<class T> 
Item<T>::Item() 
{ 
    element = 0; 
    size = 10; 
    array = new T[size]; 
} 

template<class T> 
Item<T>::~Item() 
{ 
    delete[] array; 
} 
template<class T> 
void Item<T>::add(T item) 
{ 
    if (in() == false) 
    { 
     size++; 
     array[size] = Item; 
    } 
} 

template<class T> 
void Item<T>::in(T item) 
{ 
    for (int i = 0; i < size; i++) 
    { 
     if (array[i] == Item) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
} 

template<class T> 
int Item<T>::get() 
{ 
    return element; 
} 

template<class T> 
void Item<T>::output() 
{ 
    for (int i = 0; i < size; i++) 
    { 
     cout << array[i] << endl; 
    } 
} 



int main() 
{ 





    system("pause"); 
    return 0; 

} 
+0

是否'無效輸出(T數組);''匹配模板 無效項目 ::輸出()'? – NathanOliver

+0

是的,我看到這個錯誤,我修好了 – xerror

回答

0

我繼續並糾正了所有編譯器錯誤,但請您必須重新學習您的文本框(或其他)。

我評論你的我不得不更換線路:

#include <iostream> 
using namespace std; 

template<class T> 
class Item { 
private: 
    Item(); 
    ~Item(); 
    void Add(T item); 
    int get(); 
// void output(T array); 
    void output(); 
    bool in(T item); 
    T *array; 
    int element; 
    int size; 
}; 

template<class T> 
Item<T>::Item() 
{ 
    element = 0; 
    size = 10; 
    array = new T[size]; 
} 

template<class T> 
Item<T>::~Item() 
{ 
    delete[] array; 
} 
template<class T> 
void Item<T>::Add(T item) 
//void Item<T>::add(T item) 
{ 
    if(in(item) == false) 
// if (in() == false) 
    { 
     size++; 
//  array[size] = Item; 
     array[size] = item; 
    } 
} 

template<class T> 
//void Item<T>::in(T item) 
bool Item<T>::in(T item) 
{ 
    for (int i = 0; i < size; i++) 
    { 
//  if (array[i] == Item) 
     if(array[i] == item) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
} 

template<class T> 
int Item<T>::get() 
{ 
    return element; 
} 

template<class T> 
void Item<T>::output() 
{ 
    for (int i = 0; i < size; i++) 
    { 
     cout << array[i] << endl; 
    } 
} 



int main() 
{ 
    system("pause"); 
    return 0; 

} 

但沒有你讀了編譯器的消息?我得到這個:

C02QT2UBFVH6-lm:~ gsamaras$ g++ -Wall main.cpp 
main.cpp:32:15: error: out-of-line definition of 'add' does not match any declaration in 'Item<T>'; did you mean 'Add'? 
void Item<T>::add(T item) 
       ^~~ 
       Add 
main.cpp:9:10: note: 'Add' declared here 
    void Add(T item); 
     ^
main.cpp:37:23: error: 'Item' does not refer to a value 
     array[size] = Item; 
        ^
main.cpp:5:7: note: declared here 
class Item { 
    ^
main.cpp:42:15: error: return type of out-of-line definition of 'Item::in' differs from that in the declaration 
void Item<T>::in(T item) 
~~~~  ^
main.cpp:12:10: note: previous declaration is here 
    bool in(T item); 
    ~~~~^
main.cpp:46:25: error: 'Item' does not refer to a value 
     if (array[i] == Item) 
         ^
main.cpp:5:7: note: declared here 
class Item { 
    ^
main.cpp:48:13: error: void function 'in' should not return a value [-Wreturn-type] 
      return true; 
      ^ ~~~~ 
main.cpp:52:13: error: void function 'in' should not return a value [-Wreturn-type] 
      return false; 
      ^ ~~~~~ 
main.cpp:64:15: error: out-of-line definition of 'output' does not match any declaration in 'Item<T>' 
void Item<T>::output() 
       ^~~~~~ 
7 errors generated. 
+1

謝謝你:) @gsamaras – xerror

+0

我看到一個downvote,我期待解釋。如果您對如何改進答案有任何想法,請說出口! :) – gsamaras

+0

當我在main調用這個函數時,它只是在屏幕上得到錯誤:/ – xerror

相關問題