Archived

This topic is now archived and is closed to further replies.

The C modest god

Template problem

Recommended Posts

I have the following code: template (class DATA) // Different parentheses because otherwise it dissapears class DataListLink { public: DataListLink (int Index, const DATA & Data, class DataListLink * pNext); DATA Data; int Index; class DataListLink * pNext; private: }; DataListLink(class DATA)/*other parentheses*/ ::DataListLink (int Index, const DATA & Data, class DataListLink * pNext) { } I get the following errors: d:\projects\targil\ex4\temp.h(9) : error C2079: 'Data' uses undefined class 'DATA' d:\projects\targil\ex4\temp.h(15) : see reference to class template instantiation 'DataListLink' being compiled Why I get those errors? [edited by - The C modest god on January 15, 2004 3:34:15 PM] [edited by - The C modest god on January 15, 2004 3:35:19 PM]

Share this post


Link to post
Share on other sites
If you declare it outside the class body, you need to repeat template<class DATA> in front of your constructor.

template<class DATA>
DataListLink<class DATA>::DataListLink (int Index, const DATA & Data, class DataListLink<DATA> * pNext)
{

}



“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on January 15, 2004 3:40:12 PM]

Share this post


Link to post
Share on other sites


// Make sure you have this include

#include <MyFooBar.h>

int DoSomething()
{
DataListLink <MyFooBar> list;

// yada yada

};




-----------
VenDrake

"My stupid jar is full. I can''t talk to you anymore."

Share this post


Link to post
Share on other sites
I get the same error for doing this code:


template <class DATA>
class DataListLink {
public:
DataListLink (int Index, const DATA & Data);

DATA Data;
int Index;
class DataListLink * pNext
private:
};

DataListLink <class DATA>::DataListLink (int Index, const DATA & Data)
{

}




[edited by - The C modest god on January 15, 2004 4:13:53 PM]

[edited by - The C modest god on January 15, 2004 4:14:41 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane

template <class DATA>
DataListLink<DATA>::DataListLink (int Index, const DATA & Data)
{

}



That fixed the error, but now I have 3 new errors:

d:\projects\targil\ex4\temp.h(16) : error C2371: ''DATA'' : redefinition; different basic types
d:\projects\targil\ex4\temp.h(16) : error C2039: ''__ctor'' : is not a member of ''DataListLink''
d:\projects\targil\ex4\temp.h(16) : error C2935: ''DataListLink'' : template-class-id redefined as a global function

Share this post


Link to post
Share on other sites
I noticed that the prototype for your constructor changed in your posts. Are you sure the prototype declared in the class matches the out of class definition?

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
I noticed that the prototype for your constructor changed in your posts. Are you sure the prototype declared in the class matches the out of class definition?


Yes I am sure.
Still, I dont know what cause these errors ?
How come it says that I redfine the type DATA?
Please help.

Share this post


Link to post
Share on other sites
I''m willing to bet you didn''t read my post carefully.
Correct:

template <class DATA>
DataListLink<DATA>::DataListLink (int Index, const DATA & Data)

Incorrect:

template <class DATA>
DataListLink<class DATA>::DataListLink (int Index, const DATA & Data)

(Fruny, I blame you. )

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
I''m willing to bet you didn''t read my post carefully.
Correct:

template <class DATA>
DataListLink<DATA>::DataListLink (int Index, const DATA & Data)

Incorrect:

template <class DATA>
DataListLink<class DATA>::DataListLink (int Index, const DATA & Data)

(Fruny, I blame you. )


Thanks, it works now

Share this post


Link to post
Share on other sites