0

When I'm trying to use constructor of my class, I get the following errors:

error C2955: 'myQueue' : use of class template requires template argument list

and

error C2512: 'myQueue' : no appropriate default constructor available.

This is a header file:

#ifndef myQueue_
#define myQueue_

template<typename type>
class myQueue{
public:
    myQueue();
    ~myQueue();
    type dequeue();
    void enqueue(type t);
private:
    int size;
    type* arr;
    int curSize;
};
#endif

And this is a cpp file.

#include "myQueue.h"
#include "genlib.h"

template<typename type>
myQueue<type>::myQueue()
{
    size = 10;
    arr = new type[size];
}
template<typename type>
myQueue<type>::~myQueue()
{
    delete arr[];
    arr = NULL;
}

trying to use this class here.

 int main(){
    myQueue a = new myQueue();
 }
nhgrif
  • 61,578
  • 25
  • 134
  • 173

2 Answers2

2

As explained by Wojciech Frohmberg, you must define the class in the *.h file instead of a *.cpp file, due to the fact that the code is really compiled when it's called with a specific type.

And your main is wrong.

int main(){
    myQueue<YourType>* a = new myQueue<YourType>;  // for pointer
    myQueue<YourType> b;  // for instance
 }
Caduchon
  • 4,574
  • 4
  • 26
  • 67
  • 1>Blank.obj : error LNK2019: unresolved external symbol "public: __thiscall myQueue::~myQueue(void)" (??1?$myQueue@H@@QAE@XZ) referenced in function _main 1>Blank.obj : error LNK2019: unresolved external symbol "public: __thiscall myQueue::myQueue(void)" (??0?$myQueue@H@@QAE@XZ) referenced in function _main – Michael Metreveli May 23 '15 at 12:56
0

You're not using template properly. Template classes and methods should be declared in header file, only the full specialization should be stored in source files. Your header file should therefore look like:

#ifndef myQueue_
#define myQueue_

template<typename type>
class myQueue{
public:
    myQueue();
    ~myQueue();
    type dequeue();
    void enqueue(type t);
private:
    int size;
    type* arr;
    int curSize;
};

template<typename type>
myQueue<type>::myQueue()
{
    size = 10;
    arr = new type[size];
}
template<typename type>
myQueue<type>::~myQueue()
{
    delete arr[];
    arr = NULL;
}

#endif

Also as Caduchon pointed you should declare type of your queue in a queue usage.

W.F.
  • 13,888
  • 2
  • 34
  • 81