【问题标题】:C++ Linked List printingC++ 链表打印
【发布时间】:2016-01-21 02:37:21
【问题描述】:

这是一个正向打印 LinkedList 的函数的代码。

void DLinkedList::printForwards(int age)
{
DNode * current;
current = header->next;
while (current != NULL)
{
    if (current->elem->getAge() <= age) {
        cout << current->elem->getName() << ", ";
    }
    current = current->next;
}
cout << endl;
}

这里是 DNode 类

typedef Person* Elem;
class DNode {                                 // doubly linked list node
private:
    Elem elem;                                  // node element value
    DNode* prev;                            // previous node in list
    DNode* next;                                // next node in list
    friend class DLinkedList;                   // allow DLinkedList access
};

这是 Person 类

class Person {                                 
public:
Person(int age, string first, string last){
    setAge(age);
    setFirstname(first);
    setLastName(last);
}
void setAge(int age){
    this->age = age;
}
void setFirstname(string first){
    firstName = first;
}
void setLastName(string last){
    lastname = last;
}

int getAge(){
    return age;
}

string getName(){
    return firstName + " " + lastname;
}
private:
    string firstName, lastname;
    int age;
};

这是我的主要内容

int main(){    

DLinkedList list;
Person * ryan= new Person(19, "Ryan", "Temple");
list.addFront(ryan);
list.printForwards(100);
}

这行代码给程序带来了麻烦。

if (current->elem->getAge() <= age)

elem 的值被设置为 NULL。 当电流被初始化时,它被指向正确的节点。 但是在 if 语句中访问 c​​urrent 会将其值设置为 NULL。

有人可以帮帮我吗?

编辑: 这里是 addFront 函数

void DLinkedList::add(DNode* v, Elem& e) {
DNode* u = new DNode;
u->elem = e;            // create a new node for e
u->next = v;                                // link u in between v
u->prev = v->prev;                          // ...and v->prev
v->prev->next = u;
v->prev = u;
}

添加前面

  void DLinkedList::addFront(Elem& e)     // add to front of list
{
   add(header->next, e);
}

【问题讨论】:

  • 嗯?将 elem 的值设置为 null 是什么意思?您似乎在说 elem->getAge() 将 elem 设置为空。但是,几句话之后,您还说在 if 语句中访问 c​​urrent 会将其值设置为 null。那是什么。如果 current 为 null,你怎么知道 elem 设置为 null?
  • 请贴上addFront函数的代码。这就是可能出现错误的地方。
  • @iheanyi 当前指向正确的节点,但是当我访问该元素时,它正在从 NULL 访问它。如果这有意义
  • 不完全。您无法在 null 处访问任何内容。看来您的意思是 current 不是 null 而 elem 是 null。
  • 但无论如何,就像 FrankM 建议的那样,为 addFront 发表评论。如果您遇到 elem 为 null 的问题,那很可能是源头。

标签: c++ null linked-list


【解决方案1】:

编辑:

我的回答不正确。其他事情正在发生,但没有更多源代码,我们只能推测。由于这是公认的答案,我无法删除它。因此,我已将原始内容从可见状态中删除,以免混淆可能偶然发现此问题的其他人。

【讨论】:

  • Elem 是 typedef Person*
  • @FrankM 尽管如此,当您将“对象”Elem 传递给通过非常量引用获取它的函数时,该函数会获得一个临时对象,因为编译器需要构造一个新对象并获得一个对该对象的引用。我已经编辑了答案以添加非常量参考部分,因为这是问题的症结所在。
  • 对指针的引用与指针完全一样,但让我们修改原始指针的值。代码中任何地方的临时对象都没有问题。
  • @iheanyi 我从 AddFront 和 Add 中删除了 &,它起作用了!
  • 时间不早了,我要睡觉了,但我会再尝试一次说服你。整数a = 10; int&b(a);诠释 c = b;结果是 a 是 10。b 是对包含 a 的同一内存的引用。 c 获得 a 的值,即 10。无论 a 或 b 发生什么,c 的值都将为 10,直到再次更改。将 int 替换为指针,同样适用。
猜你喜欢
  • 2016-06-15
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多