简体   繁体   中英

What does “missing template argument” mean?

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.

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.

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

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.

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

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

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.

Hope it helps.

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

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

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

collection<int> b; 

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

In collection.h put the following:

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

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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