简体   繁体   English

模板-无法将函数定义与现有声明C相匹配

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

I tried to working with templates, and I'm totaly noob with templates. 我尝试使用模板,但我对模板完全陌生。 Once I start I get a few errors, which I don't understant what does it mean ? 一旦开始,我会得到一些错误,我不明白这是什么意思? So the text is here, what I'm tring to do: 所以文字在这里,我正在努力去做:

Write a template-based class that implements a set of items. 编写一个基于模板的类,以实现一组项目。 The class should allow the user to a. 该类应允许用户使用。 Add a new item to the set. 将新项目添加到集合中。 b. b。 Get the number of items in the set. 获取集合中的项目数。 c. C。 Get a pointer to a dynamically created array containing each item in the set. 获取指向包含集合中每个项目的动态创建数组的指针。 The caller of this function is responsible for deallocating the memory. 该函数的调用者负责释放内存。

The errors is : 错误是:

Item::output': unable to match function definition to an existing declaration Item :: output':无法将函数定义与现有声明匹配

Item::in': unable to match function definition to an existing declaration Item :: in':无法将函数定义与现有声明匹配

add': is not a member of 'Item 添加”:不是“项目”的成员

And my code is here: 我的代码在这里:

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

}

I went ahead and corrected all the compiler errors, but please you have to study again your textbox (or whatever). 我继续并纠正了所有编译器错误,但请您必须再次研究文本框(或其他内容)。

I commented the lines of yours I had to replace: 我评论了您必须替换的行:

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

}

but didn't you read the compiler message? 但是您没有阅读编译器消息吗? I got this: 我懂了:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++模板:无法将函数定义与现有声明匹配 - C++ template : unable to match function definition to an existing declaration C ++,2参数类模板的部分特化:无法将函数定义与现有声明匹配 - C++, partial specialization of 2-argument class template: unable to match function definition to an existing declaration VS2015 C ++:无法将函数定义与现有声明匹配 - VS2015 C++ : unable to match function definition to an existing declaration C2244-无法将函数定义与现有声明匹配 - C2244 - unable to match function definition to an existing declaration 错误 C2244 无法将函数定义与现有声明匹配 - error C2244 unable to match function definition to an existing declaration 模板无法将函数定义与现有声明匹配 - Template unable to match function definition to an existing declaration 错误:无法使函数定义与现有声明匹配 - error:unable to match function definition to an existing declaration 无法将函数定义与现有声明匹配 - unable to match function definition to an existing declaration 无法将函数定义与cpp中的现有声明匹配 - unable to match function definition to an existing declaration in cpp 具有函数声明/原型和定义的C ++模板 - C++ Templates with function declaration/ protype and definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM