Compile-time error: expected constructor, destructor or conversion
I need some help
does this piece of 开发者_C百科code look right ?
#include "programass.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
template<class emptyValue>
struct DoublyLinkedNode
{
emptyValue data;
DoublyLinkedNode *prev, *next;
};
typedef DoublyLinkedNode<int> numListNode;
DoublyLinkedNode *listHead = NULL;
DoublyLinkedNode *listTail = NULL;
I keep getting a " expected constructor, destructor or conversion before "*" " error in debugger ? on the last two lines what have i done wrong ? :(
You did
typedef DoublyLinkedNode<int> numListNode;
but in the next two lines
DoublyLinkedNode *listHead = NULL;
DoublyLinkedNode *listTail = NULL;
you forgot to use that new numListNode
type. Change them to
numListNode *listHead = NULL;
numListNode *listTail = NULL;
Because you can't use a template class without a template argument, and you tried to do DoublyLinkedNode *listHead
when it should have been DoublyLinkedNode<something> *listHead
. So if you use your typedef
which specifies the template argument (as int
), it works.
I am sure you wanted to use numListNode instead of DoublyLinkedNode at the declaration of the two pointers.
numListNode* listHead = NULL;
numListNode* listTail = NULL;
On a side note I recommend removing the blank line between template <class emptyValue>
and struct DoublyLinkedNode
, and using the style Type* ptr;
instead of Type *ptr;
, it's nicer that way.
Seth is right, but if I understand correctly, you're trying to build a generic data type, and hence want to have list head and tail also templated. For that you need a container struct that will also hold them:
template < typename emptyValue >
struct List
{
struct DoublyLinkedNode
{
emptyValue data;
DoublyLinkedNode *prev, *next;
};
DoublyLinkedNode *head;
DoublyLinkedNode *tail;
....
}
typedef List<int> numList;
精彩评论