2016-08-01 619 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

相关问题