奇怪的C + +模板问题 [英] Weird C++ templating issues
问题描述
目前,我收到错误信息,说我的 linkList.h
中的类不是模板
../ linkList.h:34:错误:'llist'不是模板类型
../linkList.h:143:错误:'iter'不是模板类型
../josephus.cpp:14:错误:'llist'不是模板
../josephus.cpp:14:错误:聚合' llist ppl'具有不完整类型
且无法定义
../josephus.cpp:15:错误:'iter'不是
模板
b
$ blinkList.h p $ p>
模板< typename T>
class iter
{
public:
iter()
{
position = sentin;
container = sentin->有效载荷;
}
T get()const
{
assert(position!= sentin);
返回位置 - >有效载荷;
}
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;
在这件事情上的任何帮助都非常值得赞赏
解决方案您的前向声明中说
llist
是一个类:
class llist;
然后你会说这是一个模板:
template< typename T>
class llist;
类似于
iter
。
我不知道如何轻松编写它。但是,您可以在
llist $ c $内部使
节点
和iter
c>。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 templatelinkList.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
解决方案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
anditer
'inside' ofllist
.这篇关于奇怪的C + +模板问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!