"缺少模板参数"是什么意思?

Jos*_*osh 13 c++ templates

我是C++和这个网站的新手,所以肯定会有错误.当我尝试编译我的代码时,我得到的错误就像error: missing template argument before 'b'.几个小时以来,我一直在寻找世界的答案,这让我在这里.

我的任务是实现一个模板化的类Collection,它使用数组存储一个Object集合,以及集合的当前大小.

    #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;
    }
Run Code Online (Sandbox Code Playgroud)
    #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
Run Code Online (Sandbox Code Playgroud)
    #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;
    }
Run Code Online (Sandbox Code Playgroud)

Tom*_*ner 18

你必须说它是什么的集合.

template <class A> class collection {}
Run Code Online (Sandbox Code Playgroud)

要求你使用它

collection<int> b;
Run Code Online (Sandbox Code Playgroud)

或某种合适的类型.然后,它会收集整数.你没有告诉它你想要什么样的集合.


Tim*_*hko 6

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

void main { 
  T<int> t; 
  T<bool> t1; // .. etc
}
Run Code Online (Sandbox Code Playgroud)

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

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

void main { 
  T<> t;
} 
Run Code Online (Sandbox Code Playgroud)

但无论如何你应该在没有参数的情况下使用空角括号.

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

希望能帮助到你.


Mat*_*Mat 3

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

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