templates-无法将函数定义与现有的声明c++匹配
templates - Unable to match function definition to an existing declaration c++
我尝试过使用模板,但我完全不喜欢使用模板。一开始我就犯了一些错误,我不明白这是什么意思?所以文本在这里,我正在努力做的事情:
编写一个实现一组项的基于模板的类。该类应允许用户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.
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- c++类声明时,相同的例程,不同的成员变量类型
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别