C 中的排序列表

Sorted lists in C++

本文关键字:列表 排序      更新时间:2023-10-16

我正在研究数据结构课程,并已被要求创建一个排序的列表,但是,每当试图声明使用数据类型的变量时,我会遇到错误。这是我到目前为止所做的

#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

假设您是编译器。您正在从上到下读取文件,然后遇到以下内容:

List<int> x;

编译器看到了这一点时,它会进行:

wtf!我从未见过。是标识符吗?让我检查...不!在mainmain之前什么都没有!随机的胡言乱语?是的,让我们创建一个错误!

因此,编译器会产生错误。要解决此问题,您需要在main之前定义类。(您无法转发该类是一个模板,在使用之前需要定义更多信息,请在此处找到更多信息(。