对于作业,我将使用先前创建的类作为类型创建自定义单链表。
但是,在自定义列表类的任何方法中为列表创建节点时,我得到的是我的节点类型未解析。我已经包含了正确的头文件,并且位于正确的名称空间中。我不知道为什么会这样。以下是一个例子:
#include "RiderList.h"
#include "RiderNode.h"
using namespace cse274;
void RiderList::addToHead(Rider *el){
head = new RiderNode(el,head); //this is where RiderNode is unresolved
if (tail == NULL)
tail = head;
}
按要求...这里是两个标题和确切的错误:
#ifndef RIDERLIST_H_
#define RIDERLIST_H_
#include "RiderNode.h"
namespace cse274{
class RiderList{
public:
RiderList() {
head = tail = 0;
}
~RiderList();
bool isEmpty() {
return head == 0;
}
void addToHead(Rider *ride);
void addToTail(Rider *ride);
Rider *deleteFromHead(); // delete the head and return its info;
Rider *deleteFromTail(); // delete the tail and return its info;
private:
RiderList *head, *tail;
};
}
#endif /* RIDERLIST_H_ */
#ifndef RIDERNODE_H_
#define RIDERNODE_H_
#include "Rider.h"
namespace cse274{
class RiderNode{
public:
Rider *info;
RiderNode *next;
RiderNode(Rider *el, RiderNode *ptr) {
info = el;
next = ptr;
}
};
}
#endif /* RIDERNODE_H_ */
确切的错误消息:
Type 'RiderNode' could not be resolved: name resolution problem found by the indexer
有关为何会发生这种情况的任何线索? 谢谢!
答案 0 :(得分:1)
您的标头文件中有一个误导性的using namespace
指令,因此您应删除标题上标明using namespace cse274;
的行并保留namespace cse274 { ... }
。这里有关于头文件中名称空间的question,并且响应良好。
RiderNode(Rider *, RiderList *)
构造函数,但您只定义了RiderNode(Rider *, RiderNode *)
构造函数。也许您应该将RiderList中的成员head
和tail
的类型从RiderList *
更改为RiderNode *
(这取决于您对RiderList的预期设计,但在大多数情况下, list是节点链。)
答案 1 :(得分:0)
class RiderNode{
public:
Rider *info;
class RiderNode *next;
这看起来像一个问题,您不需要在该定义中使用两次类。指定RiderNode * next;没有班级应该工作。尝试删除第二节课,看看会发生什么。