【发布时间】:2018-08-06 12:53:32
【问题描述】:
我正在学习数据结构课程并被要求创建一个排序列表,但是每当我尝试声明使用数据类型的变量时都会出错。 这是我到目前为止所做的事情
#include<iostream>
using namespace std;
int main(){
List<int> x;
// Other Code for testing
}
const int MAX_ITEMS = 30;
template<class T>
class List{
private:
int size;
int currIndex;
T data[MAX_ITEMS];
public:
// list functions
};
但是在编译时出现错误:
$ g++ sortedList.cpp
sortedList.cpp:5:3: error: use of undeclared identifier 'List'
List<int> x;
^
sortedList.cpp:5:11: error: expected '(' for function-style cast or type
construction
List<int> x;
错误在于List<int> x;这一行,这不是我们应该如何使用模板声明变量吗?
完整代码可以在这里找到:https://pastebin.com/T2hXDxAP
【问题讨论】:
-
将
main()移动到底部。 -
C++ 需要前向声明。将您的
List定义移动到main之上,最好是在它自己的头文件中。
标签: c++ list data-structures