"missing template argument"是什么意思?



我对C++和这个网站很陌生,所以肯定会有错误。当我试图编译我的代码时,我会遇到类似error: missing template argument before 'b'的错误。我已经在世界上寻找答案好几个小时了,它把我带到了这里。

我的任务是实现一个模板类Collection,该类存储使用阵列的对象集合的当前大小。

    #include <iostream>
    #include "collection.h"
    using namespace std; v
    int main(int argc, char* argv[])
    {
       collection b;  //<----error missing template argument before 'b'
        return 0;
    }

    #ifndef COLLECTION_H
    #define COLLECTION_H
    #include <iostream>
    template <typename obj>
    class collection
    {
    public:
        collection();
        bool isEmpty() const;
        void makeEmpty();
        void insert(obj val);
        void remove(obj val);
        bool contains(obj val) const;
    private:
        size_t size;
        obj* col[];
    };
    #endif

    #include "collection.h"
    template <typename obj>
    collection<obj>::collection() :size(10)
    {
        col = new obj*[size];
    }
    template <typename obj>
    bool collection<obj>::isEmpty() const
    {
        for(size_t k = 0; k < size; k++)
        {
            if(col[k] != NULL)
                return false;
        }
        return true;
    }
    template <typename obj>
    void collection<obj>::makeEmpty()
    {
        for(size_t k = 0; k < size; k++)
        {
            col[k] = NULL;
        }
    }
    template <typename obj>
    void collection<obj>::insert(obj val)
    {
        int temp = 0;
        for(size_t s = 0; s < size; s++)
        {
            if(col[s] != NULL)
                temp++;
        }
        if(temp >= size)
        {
            obj* temp = new obj*[size*2];
            for(size_t c = 0; c < size; c++)
                temp[c] = col[c];
            delete col;
            col = temp;
        }
        else
            col[temp] = val; 
    }
    template <typename obj>
    void collection<obj>::remove(obj val)
    {
        for(size_t x = 0; x < size; x++)
        {
            if (col[x] == val)
            {
                for(size_t y = x; y < size-1; y++)
                {
                    col[y] = col[y+1];
                }
                col[size-1] = NULL;
                return;
            }
        }
    }
    template <typename obj>
    bool collection<obj>::contains(obj val) const
    {
        for(size_t z = 0; z < size; z++)
        {
            if(col[z] == val)
                return true;
        }
        return false;
    }

你必须说明它是什么的集合。

template <class A> class collection {}

要求您将其用作

collection<int> b;

或者某种适当的类型。这就形成了int的集合。你还没有告诉它你想要一个集合。

第一个:按类型实例化模板。所以如果你有template <typename obj> class T {...};,你应该像一样使用它

void main { 
  T<int> t; 
  T<bool> t1; // .. etc
}

您可以为类模板声明中定义的typename参数使用具有默认值的模板

template <typename obj = int> class T {/*...*/};
void main { 
  T<> t;
} 

但是不管怎样,在不带参数的情况下使用它时,应该放上空的尖括号。

第二个:在声明模板时,将其整个放置在头文件中。他的方法的每个定义都应该在文件"*.h"中,不要问我为什么,只是不要将其拆分为头和"cpp"文件。

希望能有所帮助。

好吧,您缺少一个模板参数。您不能创建collection对象,它只是一个模板。

您只能创建例如collection<int>collection<std::string>

您需要指定模板的类型,如int或其他类型:

collection<int> b; 

您的代码中存在大量错误。首先在头文件中定义模板:

在collection.h中放入以下内容:

#ifndef COLLECTION_H
#define COLLECTION_H
    template <typename obj>
    class collection
    {
    public:
      collection() {}
        bool isEmpty() const;
        void makeEmpty();
        void insert(obj val);
        void remove(obj val);
        bool contains(obj val) const;
    private:
        size_t size;
        obj* col[];
    };
#endif

然后在主函数内的.cpp文件中执行以下操作:

  collection<int> b;

您可以使用不同的类型来代替int,但要点是您需要一个类型来实例化模板。

相关内容

最新更新