tags:

views:

402

answers:

3

So basically the assignment was we had to create a doubly linked list that's templated generically instead of locked to a single data type. I've tried compiling both with gcc and msvc and both compilers are giving me roughly the same errors so I'm assuming its just my bad coding and not the quirkyness of one compiler or the other.

Currently, I'm getting errors saying that my classes in linkList.h are not a template

../linkList.h:34: error: ‘llist’ is not a template type<br>
../linkList.h:143: error: ‘iter’ is not a template type<br>
../josephus.cpp:14: error: ‘llist’ is not a template<br>
../josephus.cpp:14: error: aggregate ‘llist ppl’ has incomplete type and cannot be defined<br>
../josephus.cpp:15: error: ‘iter’ is not a template<br>

linkList.h

template<typename T>
class iter
{
public:
iter()
{
 position = sentin;
 container = sentin->payload;
}

T get() const
{
 assert(position != sentin);
 return position->payload;
}

void next()
{
 position = position->next;
}

void previous()
{
 position = position->prev;
}

bool equals(iter itr) const
{
 return position == itr.position;
}
private:
node *position;
llist *container;
};

josephus.cpp

llist<int> ppl;
iter<int> pos;

int start = static_cast<int>(argv[1]) - 1;
int end = static_cast<int>(argv[2]) - 1;

Any help in this matter is much appreciated

+4  A: 

Your forward declaration says llist is a class:

class llist;

Then you say it is a template:

template<typename T>
class llist;

Similarly with iter.

I don't know how you could make it compilable easily. However, you can make node and iter 'inside' of llist.

strager
Ah crap, well that does make sense now that I see the answer. Can't believe I made an error like that, thanks.
by "inside," I believe he means private to the llist class. This is how I would do it.
Hooked
Here's a link to a very similar question elsewhere on SO. I found this question (and your correct answer) when researching the other. http://stackoverflow.com/questions/1590688/class-is-not-a-template-type
sgreeve
(So have a +1) ;)
sgreeve
+2  A: 

The llist is not a class. So forward declaring it is not usefull.

template<typename T> class llist;

Trying to make the code compile is relatively simple.
You have just missed the template part of a lot of the types. Search for iter llist and node and make sure they have the appropriate on the end.

If you look at the STL it is conventinal to typedef some internal types for ease of use. You could follow the same principle.

template<typename T>
class llist
{
     typedef iter<T>  Iter;
     typedef node<T>  Node;

      // The rest of the code.
};
Martin York
+2  A: 

There are several issues.

class A;

is not the way you forward declare a templated class.

If A has a single templated parameter you need to say:

template<typename T>
class A;

If you say that after you've already said class A; you're contradicting yourself. The next issue is simlar, friend class A; if A is templated won't work, you need to say friend class A<T>; or similar. Finally, static_cast<int>(argv[1]) will not compile (althought static_cast<int>(argv[1][0]) would, but is still not want you want). To convert a string to an integer meaningfully, you'll need to use atoi, strtol, stringstream etc.

Logan Capaldo
Thanks for that heads up on atoi, that was one of the errors I was preparing to work on