奇怪的C + +模板问题 [英] Weird C++ templating issues

查看:157
本文介绍了奇怪的C + +模板问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上这个任务是我们必须创建一个双向链表,这个链表通常是模板化的,而不是锁定到单个数据类型。我试着用gcc和msvc编译,两种编译器都给我大致相同的错误,所以我假设它只是我的错误编码,而不是一个编译器或另一个编译器的古怪。



目前,我收到错误信息,说我的 linkList.h 中的类不是模板


../ linkList.h:34:错误:'llist'不是模板类型

../linkList.h:143:错误:'iter'不是模板类型

../josephus.cpp:14:错误:'llist'不是模板

../josephus.cpp:14:错误:聚合' llist ppl'具有不完整类型
且无法定义
../josephus.cpp:15:错误:'iter'不是
模板


b
$ b

linkList.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 节点 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 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

解决方案

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.

这篇关于奇怪的C + +模板问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆