Jump to content
  • Advertisement

Archived

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

Qa303asmGuru

template hell....please help!!!

This topic is 5473 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

Last year I wrote a doubly linked list templatized class for a programming course. It worked fine and I did very well on the project. Well now I want to use this class for the game I''m working on, and I''m getting some annoying compiler errors that I cannot figure out. I had it working perfectly and i have no idea what happened. Anyway... This is how I set up the dnode class template.
// The templatized dnode class

template <class Type>
class dnode
{
public:
 ...    // leaving out stuff that is irrelevant

private:
 ...
};
Here is one global template function prototype for use with dnode
template <class Type>
bool 
list_empty(const dnode<Type>* head_ptr);
  // returns TRUE if list !empty

  // returns FALSE if list is empty

...and the definition
template <class Type>
bool 
list_empty(const dnode<Type>* head_ptr)
{	
	if (head_ptr == head_ptr->next())
		return true;

	return false;
}
...and finally, the kicker. Here is the error I get:
e:\CSE 120\Lab 7\dnode.cpp(19): error C2143: syntax error : missing '','' before ''<''
 
WTF??? It worked fine before. Another error I get is:
e:\CSE 120\Lab 7\dnode.cpp(42): error C2061: syntax error : identifier ''dnode''
 
...when I try to define another global function as
template <class Type>
void 
list_head_insert(dnode<Type>* head_ptr, const Type& entry)
{
   // blah

}
Can anybody tell me what the hell happened? Thanks. -Q | My Unfinished Page | | I need answers! | | Genaside presents: Hazard Ball | | Maddox |

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
MonkeyChuff is right. Either they''re in the same file or you #include the implementation file at the bottom of the header file.

Share this post


Link to post
Share on other sites
Since we can''t see the code causing the problems, how are we meant to provide answers?

Of course, the real answer is to use std::list.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The error is the same one you would get from a missing template defintion. That is it can''t find the header file in which the templated class is defined. Or, through the use of marco guards its not being included, check your marcos guards to make sure your not resusing the same guard labels.

You should check its included correctly.

-ddn

Share this post


Link to post
Share on other sites
from what I see, dnode is undefined at the place where u got your template function, simply putting the dnode def infront of the functions def and impl should solve it.

Share this post


Link to post
Share on other sites
Wow, this one is definetly mind boggling.

Anyway, I'll try to do a better job of making things as clear as possible.

Before I post code, I would like to point out that I HAVE included the implementation file at the end of the header, and have not tried to include the header in the implementation (basically, at the end of dnode.h I have #include "dnode.cpp").

The dnode doubly linked list class...

#ifndef _DNODE_H
#define _DNODE_H
#include <cstdlib> // Provides size_t and NULL



template <class Type>
class dnode
{
public:

// CONSTRUCTOR

dnode( const Type& init_data = Type( ),
dnode<Type>* prev_link = NULL,
dnode<Type>* next_link = NULL )
{
data_field = init_data;
prev_field = prev_link;
if (prev_field==NULL)
prev_field = this;
next_field = next_link;
if (next_field==NULL)
next_field = this;
}

// Member functions to set the data and link fields:

void set_data(const Type& new_data) { data_field = new_data; }
void set_prev_link(dnode<Type>* prev) { prev_field = prev; }
void set_next_link(dnode<Type>* next) { next_field = next; }

// Two slightly different member functions to retrieve the current data:

Type data( ) const { return data_field; }
Type& data( ) { return data_field; }

// Two slightly different member functions to retrieve

// the prev link:

const dnode<Type>* prev( ) const { return prev_field; }
dnode<Type>* prev( ) { return prev_field; }

// Two slightly different member functions to retrieve

// the c link:

const dnode<Type>* next( ) const { return next_field; }
dnode<Type>* next( ) { return next_field; }

private:
Type data_field;
dnode<Type> *prev_field, // pointer to previous node

*next_field; // pointer to next node

};

// FUNCTIONS for the DLCLWDHN toolkit

template <class Type>
bool
list_empty(const dnode<Type>* head_ptr);

template <class Type>
size_t
list_length(const dnode<Type>* head_ptr);

template <class Type>
void
list_head_insert(dnode<Type>* head_ptr, const Type& entry);

template <class Type>
void
list_insert(dnode<Type>* curr_ptr, const Type& entry);

template <class DNodePtr, class Type>
DNodePtr
list_search(DNodePtr head_ptr, const Type& target);

template <class DNodePtr, class SizeType>
DNodePtr
list_locate(DNodePtr head_ptr, SizeType position);

template <class Type>
void
list_head_remove(dnode<Type>* head_ptr);

template <class Type>
void
list_remove(dnode<Type>* curr_ptr);

template <class Type>
void
list_clear(dnode<Type>* head_ptr);

template <class Type>
size_t
list_occurrences(const dnode<Type>* head_ptr, const Type& target);

template <class Type>
void
list_tail_attach(dnode<Type>* head_ptr, const Type& entry);

template <class Type>
void
list_tail_remove(dnode<Type>* head_ptr);

template <class Type>
dnode<Type>*
list_copy( const dnode<Type>* source_ptr);

template <class Type>
dnode<Type>*
list_copy_front( const dnode<Type>* source_ptr, size_t n);

template <class Type, class StringType>
void
list_display ( const dnode<Type>* head_ptr,
const StringType separator = StringType(" "));

template <class Type, class StringType>
void
list_display_reverse
( const dnode<Type>* head_ptr,
const StringType separator = StringType(" "));


#include "dnode.cpp"

#endif


...and the imp file dnode.cpp

#include <iostream>
#include <cassert> // Provides assert

#include <cstdlib> // Provides NULL and size_t

#include <math.h> // Provides fabs()


using namespace std;

template <class Type>
bool // Error 1

list_empty(const dnode<Type>* head_ptr)
{
if (head_ptr == head_ptr->next())
return true;

return false;
}

template <class Type>
size_t // Error 2

list_length(const dnode<Type>* head_ptr)
{
const dnode<Type>* cursor = head_ptr;
size_t count = 0;

for (cursor; cursor != head_ptr->prev(); cursor = cursor->next())
++ count;

return count;
}

template <class Type>
void // Error 3

list_head_insert(dnode<Type>* head_ptr, const Type& entry)
{
dnode<Type>* new_link = new dnode<Type>;

new_link->set_data(entry);
new_link->set_next_link(head_ptr->next());
new_link->set_prev_link(head_ptr);
head_ptr->set_next_link(new_link);
new_link->next()->set_prev_link(new_link);
}

template <class Type>
void // Error 4

list_insert(dnode<Type>* current_ptr, const Type& entry)
{
dnode<Type>* temp_ptr = new dnode<Type>;

temp_ptr->set_data(entry);

temp_ptr->set_next_link(current_ptr->next());
temp_ptr->set_prev_link(current_ptr);

current_ptr->next()->set_prev_link(temp_ptr);

current_ptr->set_next_link(temp_ptr);
}

template <class DNodePtr, class Type>
DNodePtr // Error 5

list_search(DNodePtr head_ptr, const Type& target)
{
DNodePtr temp = head_ptr->next();

while (head_ptr != temp)
{
// traverse the list until target is found (if target exists)

if (temp->data() == target)
return temp;

temp = temp->next();
}

return NULL;
}

template <class DNodePtr, class SizeType>
DNodePtr // Error 6

list_locate(DNodePtr head_ptr, SizeType position)
{
assert(position > 0);

DNodePtr temp = head_ptr->next();
SizeType count = 0;

while (head_ptr != temp)
{
count++;

if (count == position)
return temp;

temp = temp->next();
}

return NULL;
}

template <class Type>
void // Error 7

list_head_remove(dnode<Type>* head_ptr)
{
dnode<Type>* temp = head_ptr->next();

head_ptr->set_next_link(temp->next());

temp->next()->set_prev_link(head_ptr);

// destroy old node

delete temp;
}

template <class Type>
void
list_remove(dnode<Type>* current_ptr)
{
current_ptr->prev()->set_next_link(current_ptr->next());

current_ptr->next()->set_prev_link(current_ptr->prev());

// destroy old node

delete current_ptr;
}

template <class Type>
void // Error 8

list_clear(dnode<Type>* head_ptr)
{
dnode<Type>* temp = head_ptr->next();

while (head_ptr != temp)
{
head_ptr->set_next_link(temp->next());

temp->next()->set_prev_link(head_ptr);

// destroy old node

delete temp;

temp = head_ptr->next();
}
}

template <class Type>
size_t // Error 9

list_occurrences( const dnode<Type>* head_ptr, const Type& target)
{
size_t count = 0;

const dnode<Type>* temp_ptr = head_ptr->next();

while (temp_ptr != head_ptr)
{
if (is_equal(temp_ptr->data(), target))
count ++;

temp_ptr = temp_ptr->next();
}

return count;
}

template <class Type>
void // Error 10

list_tail_attach(dnode<Type>* head_ptr, const Type& entry)
{
dnode<Type>* new_link = new dnode<Type>;

new_link->set_data(entry);
new_link->set_next_link(head_ptr);
new_link->set_prev_link(head_ptr->prev());
head_ptr->set_prev_link(new_link);
new_link->prev()->set_next_link(new_link);
}

template <class Type>
void // Error 11

list_tail_remove(dnode<Type>* head_ptr)
{
dnode<Type>* temp = head_ptr->prev();

head_ptr->set_prev_link(temp->prev());

temp->prev()->set_next_link(head_ptr);

// destroy old node

delete temp;
}

template <class Type>
dnode<Type>*
list_copy( const dnode<Type>* source_ptr)
{
dnode<Type>* new_head_ptr = new dnode<Type>;

for(const dnode<Type>* cursor = source_ptr->next();cursor != source_ptr; cursor = cursor->next())
list_tail_attach(new_head_ptr, cursor->data());

return new_head_ptr;
}

template <class Type>
dnode<Type>*
list_copy_front( const dnode<Type>* source_ptr, size_t n)
{
dnode<Type>* new_head_ptr = new dnode<Type>;
const dnode<Type>* cursor = source_ptr->next();
size_t count = 0;

for(size_t count = 0; count < n; count++)
{
list_tail_attach(new_head_ptr, cursor->data());
cursor = cursor->next();
}

return new_head_ptr;
}

template <class Type, class StringType>
void
list_display (const dnode<Type>* head_ptr, StringType separator)
{
if (list_empty (head_ptr) ) return;

const dnode<Type>* p = head_ptr->next();
do {
cout << p->data() << separator;
p = p->next();
}
while (p != head_ptr);
}

template <class Type, class StringType>
void
list_display_reverse ( const dnode<Type>* head_ptr,
const StringType separator)
{
if (list_empty (head_ptr) ) return;

const dnode<Type>* p = head_ptr->prev();
do {
cout << p->data() << separator;
p = p->prev();
}
while (p != head_ptr);
}

template <class Type>
bool
is_equal(const Type A, const Type B)
{
// function used in attempt to eliminate floating point error


double tolerance = .000001;

double diff = A - B;

if (fabs(diff) < tolerance)
return true;

return false;
}


Now for the compiler errors:


e:\CSE 120\Lab 7\dnodetest\dnode.cpp(16): error C2143: syntax error : missing ',' before '<'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(26): error C2143: syntax error : missing ',' before '<'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(39): error C2061: syntax error : identifier 'dnode'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(52): error C2061: syntax error : identifier 'dnode'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(108): error C2061: syntax error : identifier 'dnode'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(122): error C2061: syntax error : identifier 'dnode'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(134): error C2061: syntax error : identifier 'dnode'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(153): error C2143: syntax error : missing ',' before '<'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(172): error C2061: syntax error : identifier 'dnode'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(185): error C2061: syntax error : identifier 'dnode'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(198): error C2143: syntax error : missing ';' before '<'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(198): error C2501: 'dnode' : missing storage-class or type specifiers
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(198): error C2988: unrecognizable template declaration/definition
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(198): error C2059: syntax error : '<'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(212): error C2143: syntax error : missing ';' before '{'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(212): error C2447: '{' : missing function header (old-style formal list?)
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(226): error C2954: template definitions cannot nest
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(228): error C2143: syntax error : missing ',' before '<'
e:\CSE 120\Lab 7\dnodetest\dnode.cpp(242): error C2143: syntax error : missing ',' before '<'


I've indicated most of the lines the errors refer to. It would appear that the 'dnode' type is just not being recognized by the compiler, even though the .h and .cpp are included in the same project.

Hope this helps solve the caper.

-Q

[edited by - qa303asmguru on September 24, 2003 1:20:50 PM]

Share this post


Link to post
Share on other sites
Update:

Well, I copied and pasted all of the defs into the header, and I can compile with no errors.

So, I''m guessing I''m doing something wrong with including the the impl file at the end of the def (which worked fine before).

I''d still rather keep my class defs and implementations in a separate file...anybody see what I''m doing wrong with this line at the end of the header?


#include "dnode.cpp"

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!