templates-无法将函数定义与现有的声明c++匹配

templates - Unable to match function definition to an existing declaration c++

本文关键字:声明 c++ 匹配 函数 定义 templates-      更新时间:2023-10-16

我尝试过使用模板,但我完全不喜欢使用模板。一开始我就犯了一些错误,我不明白这是什么意思?所以文本在这里,我正在努力做的事情:

编写一个实现一组项的基于模板的类。该类应允许用户a.向集合中添加一个新项目。b.获取集合中的项目数。c.获取一个指向动态创建的数组的指针,该数组包含集合中的每个项。这个此函数的调用方负责释放内存。

错误是:

项::输出':无法将函数定义与现有声明匹配

"中的项::无法将函数定义与现有声明匹配

add":不是"Item 的成员

我的代码在这里:

#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;
}

我继续纠正了所有编译器错误,但请您重新研究您的文本框(或其他)。

我评论了你的行,我必须替换:

#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.