【问题标题】:templates - Unable to match function definition to an existing declaration c++模板 - 无法将函数定义与现有声明 C++ 匹配
【发布时间】:2016-08-01 17:18:44
【问题描述】:

我尝试使用模板,但我完全不喜欢模板。一旦我开始我会得到一些错误,我不明白这是什么意思? 所以文字就在这里,我想做的事:

编写一个基于模板的类来实现一组项目。班级应该允许 用户要 一种。将新项目添加到集合中。 湾。获取集合中的项目数。 C。获取指向包含集合中每个项目的动态创建数组的指针。这 该函数的调用者负责释放内存。

错误是:

Item::output': 无法将函数定义与现有声明匹配

Item::in': 无法将函数定义与现有声明匹配

添加:不是“项目”的成员

我的代码在这里:

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

}

【问题讨论】:

  • void output(T array); 是否匹配 template&lt;class T&gt; void Item&lt;T&gt;::output()
  • 是的,我看到了这个错误,我修复了它

标签: c++ class templates compiler-errors syntax-error


【解决方案1】:

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

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

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

【讨论】:

  • 我看到了一个反对意见,我希望得到解释。如果您对如何改进答案有任何想法,请说出来! :)
  • 当我在 main 中调用此函数时,它只会在屏幕上显示错误:/
  • @xerror 好吧,我修复了编译器错误,您必须尝试弄清楚逻辑错误会发生什么,如果需要,发布一个新问题! :)
  • @xerror 谢谢。但是,我并没有说你给了我一个,但一般来说,在投票时,应该评论为什么。
猜你喜欢
  • 2016-11-19
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多