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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9195749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:27:46  来源:igfitidea点击:

What does "missing template argument" mean?

c++templates

提问by Josh

I'm pretty new to C++ and this site so there are bound to be errors. When I try to compile my code I get errors like error: missing template argument before 'b'. I've been searching the world for answers for hours and it has led me here.

我对 C++ 和这个站点很陌生,所以肯定会有错误。当我尝试编译我的代码时,我收到类似error: missing template argument before 'b'. 几个小时以来,我一直在世界各地寻找答案,这让我来到了这里。

My assignment is to implement a templated class Collection that stores a collection of Objects using an array, along with the current size of the collection.

我的任务是实现一个模板化类 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;
    }

回答by Tom Tanner

You have to say what it's a collection of.

你必须说它是一个集合。

template <class A> class collection {}

requires that you use it as

要求您将其用作

collection<int> b;

or some appropriate type. That then makes a collection of ints. You haven't told it what you want a collection of.

或一些合适的类型。然后生成一个整数集合。你还没有告诉它你想要收集什么。

回答by Tim Kachko

First: Instantiate template by type. So if you have template <typename obj> class T {...};you should use it like

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

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

You can use a template with default value for the typename parameter defined in the class template declaration

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

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

void main { 
  T<> t;
} 

but anyway you should put empty angle brackets when use it without parameter.

但无论如何你应该在不带参数的情况下使用它时放置空尖括号。

Second: While declaring template, place it whole in the header file. Each definition of his methods should be in the file "*.h", don't ask me why, just don't split it to the header and "cpp" file.

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

Hope it helps.

希望能帮助到你。

回答by Mat

Well, you're missing a template argument. You can't create a collectionobject, that's just a template.

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

You can only create e.g. a collection<int>or collection<std::string>.

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

回答by Bojan Komazec

You need to specify the type for template, like intor some other type:

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

collection<int> b; 

回答by Sid

There are a large number of errors in your code. First define your template in a header file:

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

In collection.h put the following:

在 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

Then in a .cpp file inside a main function do the following:

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

  collection<int> b;

Instead of int you can use a different type but the main point is that YOU NEED A TYPE to instantiate a template.

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