Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird C++ templating issues

Tags:

c++

c

gcc

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
../linkList.h:143: error: ‘iter’ is not a template type
../josephus.cpp:14: error: ‘llist’ is not a template
../josephus.cpp:14: error: aggregate ‘llist ppl’ has incomplete type and cannot be defined
../josephus.cpp:15: error: ‘iter’ is not a template

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

like image 952
user161741 Avatar asked Aug 23 '09 23:08

user161741


2 Answers

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.

like image 198
strager Avatar answered Oct 11 '22 14:10

strager


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.

like image 32
Logan Capaldo Avatar answered Oct 11 '22 13:10

Logan Capaldo