同一个结构体中的元素 不能同时传递给一个指针

[复制链接]
查看11 | 回复1 | 2011-6-5 16:00:05 | 显示全部楼层 |阅读模式
下面是程序,是十字链表的,应该可以运行,其中有我加入了输出 行列序 ,我遇到的问题是同一个结构体里的元素在指针传递的时候没有都传递,不知道什么原因,输出的col和item是对的,而row不对。谢谢各位了~
#include
using namespace std;
int main()
{
        matrix M1;
}
enum boolean{FALSE,TRUE};
struct element{int row,col;float item;};
class matrix;
class node
{
        friend class matrix;
        node()
        {
                head=FALSE;
                down=right=NULL;
        }
        node(boolean b,element *t);
private:
        node *down,*right;
        boolean head;
        union
        {
                element triple;
                node *next;
        };
};
node::node(boolean b,element *t)
{
        head=b;
        if(b)
        {
                right=down=this;
        }
        else triple=*t;
}
//typedef node *nodeptr;
class matrix
{
private:
        node *headnode;
public:
        matrix();
        ~matrix();
};
matrix::matrix()
{
        headnode=new node(TRUE,0);
        headnode->triple.row=0;
        headnode->triple.col=0;
        headnode->triple.item=0;
        int irow=1,icol=1,i=1;
        int rows,cols,terms;
        int iitem;
        cout>rows>>cols;
        cout>terms;
        cout next;
        coutiitem;
                        if(iitem)
                        {
                                p=new node();
                                before->next=p;
                                p->triple.row=irow;
                                p->triple.col=icol;
                                p->triple.item=iitem;
                                couttriple.itemtriple.rowtriple.coltriple.row==p->triple.row)
                                {
                                        before->right=p;
                                        couttriple.col==p->triple.col)
                                {
                                        before->down=p;
                                        coutnext;
                                p=p->next;
                                i++;
                        }
                }
                p=headnode->next;
                for(i=0;itriple.itemdown;
                }
}
matrix::~matrix()
{
        node *d=headnode->next;
        node *p=d;
        delete []headnode;
        for(;!d;)
        {
                p=d;
                d=d->next;
                delete []p;
        }
}

回复

使用道具 举报

千问 | 2011-6-5 16:00:05 | 显示全部楼层
错误发生在几个位置1:node *before=headnode;node *p=before->next;这2句用before指针保存了headnode指针,注意headnode有一个共用体成员union{element triple;node *next;};而之前headnode对triple有指定值的过程headnode->triple.row=0;headnode->triple.col=0;headnode->triple.item=0;这样导致了headnode的共用体的next虽然是指向的triple的地址,但是实际上不是你需要访问的next内容,这里由于是共用体...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行