繁体   English   中英

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

[英]What does “missing template argument” mean?

我是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;
    }

    #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;

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

第一 :按类型实例化模板。 所以如果你有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

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

  collection<int> b;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM