我正在处理的类在这个项目中遇到错误。错误是
[错误] 在没有参数列表的情况下无效使用模板名称"SimpleListContainer">
//Application.cpp
#include <iostream>
#include "SimpleListContainer.cpp"
using namespace std;
int main()
{
SimpleListContainer obj1;
}
这是另一个文件
//#pragma once
#include <iostream>
using namespace std;
template <typename T>
class SimpleListContainer {
private:
int size;
const static int capacity = 5;
T data[capacity];
public:
SimpleListContainer();//which will add an item of type T to our array and return true, or return false if array already full
// bool insert(T);//which will return true / false depending on if value T is present in array
// bool search(T);//which will return the number of items currently stored(different from capacity which is fixed)
int length();//which returns true if list is empty(size 0) or else false
bool empty();//which prints all the data stored, line by line for each element in the array
void print();//which clears the list by resetting size to 0
void clear();//which deletes all instances of T found in the list and compresses the list via a simple algorithm
// void remove(T);
};
SimpleListContainer::SimpleListContainer()
{
size = 0;
}
我只需要知道我做错了什么。这是我第一次在程序中使用模板,所以我根本不理解它,我找到的在线资源并没有解决我遇到的问题。
我只需要知道我做错了什么。
实例化类模板时未传递 type 参数。
像这样的类模板:
template<typename T>
class MyTemplate { public: T t; };
使用时需要填写T
:
int main () {
MyTemplate<int> o { 42 };
std::cout << o.t;
}
您应该回到C++书并重新阅读模板章节。
关于如何单独定义模板类函数存在错误。 而不是
SimpleListContainer::SimpleListContainer()
{
你需要写:
template<typename T>
SimpleListContainer<T>::SimpleListContainer()
{