C++中的可比较项数组

Array of Comparables in C++

本文关键字:数组 可比较 C++      更新时间:2023-10-16

我的代码中出现以下错误。我对C++非常生疏,不确定自己做错了什么。

错误消息:

Error: Field has incompatible type 'int []'   

代码:

template<typename Comparable> class OrderedCollection
{         
private:
    Comparable data[];  //ERROR CAUSED BY THIS LINE
    int _size;
    int _current;
    const int MAX_SIZE = 100;

对此可能的修复方法是使用第二个接受size_t的模板参数。

template<typename Comparable, size_t MAX_SIZE = 100> class OrderedCollection
    {
    private:
        Comparable data[MAX_SIZE];  //Error should be gone
        int _size;
        int _current;

您需要指定const数组大小,数组大小必须在编译时已知。

像这样的东西应该修复:

  Comparable data[MAX_SIZE];