-2

我一直在使用 bsd-list。我编写了一个非常简单的程序,通过创建列表来插入简单的整数元素。代码如下。

#include<iostream>
#include<stdlib.h>
#include"bsd-list.h"

using namespace std;

struct foo {
    int a;
    LIST_ENTRY(foo) pointers; // pointers is the object of the structure generated by List Entry
} *temp, *var, *ptr;

LIST_HEAD(foo_list, foo);

int main(void)
{
    LIST_HEAD(foo_list, foo) head;
    LIST_INIT(&head);

    struct foo *item1 = new foo;
    struct foo *item2 = new foo;
    struct foo *item3 = new foo;
    item1->a = 60;
    item2->a = 120;
    item3->a = 240;
    LIST_INSERT_HEAD(&head, item1, pointers);
    LIST_INSERT_AFTER(item1, item2, pointers);
    LIST_INSERT_BEFORE(item2, item3, pointers);

    //Displaying inner details of list
    {
        cout<<"HEAD's Address : "<<head.lh_first<<endl;
        cout<<"Item 1 next value : "<<(item1)->pointers.le_next<<endl;
        cout<<"Item 1 prev value : "<<*(item1)->pointers.le_prev<<endl;
        cout<<"HEAD's Address : "<<head.lh_first<<endl;
        cout<<"Item 2 next value : "<<item2->pointers.le_next<<endl;
        cout<<"Item 2 prev value : "<<*(item1)->pointers.le_prev<<endl;
        cout<<"HEAD's Address : "<<head.lh_first<<endl;
        cout<<"Item 3 next value : "<<item3->pointers.le_next<<endl;
        cout<<"Item 3 prev value : "<<*(item3)->pointers.le_prev<<endl;
    }

    ptr = head.lh_first;
    for(;;ptr = ptr->pointers.le_next)
    {
        cout<<ptr->a<<endl;
    }

    return (0);
}

使用语句*(item1)->pointers.le_prev我在le_prev.

但是我想做类似的事情,**(item1)->pointers.le_prev这样我就得到了 60 的值。但我遇到了错误。正确使用解引用的正确语法是什么?

4

2 回答 2

0

您尚未<<为您的结构定义流输出运算符 , 。

std::ostream& operator<< (std::ostream& stream, const foo& data)
{
    return stream << data.a;
}
于 2013-09-09T11:25:17.893 回答
0

您需要检查 NULL :

for(;;ptr = ptr->pointers.le_next)
{
    if(ptr==NULL) break;
    cout<<ptr->a<<endl;
}
于 2013-09-09T10:51:06.640 回答