Sign in to follow this  

template error C2512

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've just experimented with Templates for the first time, by creating a Linked List Template. I've got the following code:

//Linked List Template Class
template <typename type>
struct listinfo
{
	type * data;
	listinfo * next;
	listinfo * prev;
};

template <typename type>
typedef listinfo<type> node;

template <typename type>
class LinkedList
{
private:
	node * head;
	node * tail;

  //Add the new node after oldnode;
	void addAfter(node * oldnode, node * newnode);
	void prepend(node * newnode);
	node * findNode(int n);
public:
	LinkedList();	
	type &add(int addtype);
};

and the implementations of course.. But when I create a new object and try using the add() function, it creates the error on this line of code: node * newnode = new node; saying, "error C2512: 'listinfo<type>' : no appropriate default constructor available"

Share this post


Link to post
Share on other sites
Hello you cannot template typedefs, the way around this is to typedef it within the class template e.g.


//Linked List Template Class
template <typename type>
struct listinfo {
type* data;
listinfo* next;
listinfo* prev;
};

template <typename type>
class LinkedList {
public:

typedef listinfo< type > node;
typedef node* node_ptr

private:
node_ptr head;
node_ptr tail;

public:
LinkedList();
void add(const type& t);
};

Share this post


Link to post
Share on other sites
Quote:
Original post by squicklid
well that fixes it inside the class {}'s, but for the implementation of the member functions it tells me that node is undefined.


If you've implementated the member functions out side of the class template then you have two ways to use that node e.g.


template< typename T >
struct node {};

template< typename T >
struct linked_list {

typedef node<T>* node_ptr;

void add(node_ptr);
};








method one (indirectly using the typedef):

template< typename T >
void linked_list<T>::add(typename linked_list<T>::node_ptr n_ptr) {
//...add imp
}


method two (directly using the original node type):

template< typename T >
void linked_list<T>::add(node<T>* n_ptr) {
//...add imp
}

Share this post


Link to post
Share on other sites
Quote:
Original post by squicklid
why can't I just use

#define node listinfo<type>


Well if you do that its not very useful as there must be a type called "type" available in the context of it's use.

You should limit your use of marcos to bare minimum necessities there not evil(but they can be) there just not type safe, macros don't care about c++ code they just replace text so other people may frown upon looking at your code.

If you still wonted to use a macro to do that then aleast let it take a parameter e.g.


#define node(type) listinfo< type >

Share this post


Link to post
Share on other sites

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this