注册 登录
编程论坛 C++教室

【求助帖】类中定义类指针 - 各种指针。。。。

coleev 发布于 2013-01-08 15:45, 3208 次点击
代码如下
程序代码:
//De Co+Live
#include <iostream>
using namespace std;
#include <string>
#include <stdlib.h>
//
class sample
{
    private:
    sample* front;//指向逻辑上前一个对象的指针
    sample* next;//指向逻辑上后一个对象的指针
    static int iCounter;//计数器 - 有多少个同类对象被创建
    int iSerial;//本对象在第几个
    public:
    sample* lastCreated;//最后一个被创建的对象的地址
    sample();
    friend ostream& operator<<(ostream&,sample&);//重载<<运算符   
};
//
int sample::iCounter=0;//初始化iCounter为0
sample::sample()
{
    iCounter++;
    if(iCounter==1)
    {
        front=NULL;
        lastCreated=this;
        next=NULL;
        iSerial=1;   
    }
    else
    {
        front=lastCreated;
        lastCreated->next=this;
        lastCreated=this;
        next=0;
        iSerial=iCounter;
    }
}
//
ostream& operator<<(ostream& OUT,sample& SAMPLE)
{
    OUT<<"本身第"<<SAMPLE.iSerial<<endl;
    OUT<<"总共"<<SAMPLE.iCounter<<endl;
}
//****************************************
//
//main()开始
//
//****************************************
int main()
{
    sample sample1;
    cout<<sample1<<endl;
    sample sample2;
    cout<<sample2<<endl;
    system("pause");
    return 0;
}


思路就是想定义一个sample类,若新建一个sample类对象a,那么a的iSerial为1,其他指针各自更新;又创建一个sample类b对象,b就排在第二,a.next就指向b,如此往复,就是一个链表。

创建一个对象时还没问题,第二个就有问题了。

求解
5 回复
#2
rjsp2013-01-08 16:34
很烂,不多说,多说人不喜
只说,lastCreated 应该加 static
#3
coleev2013-01-08 19:47
回复 2楼 rjsp
原本构思的时候也是想的加static,后来忘了;现在加了,编译的时候出现"Undefined reference to sample::lastCreated"

为毛会出现这种情况啊?

Ps:这段码也只是为了检验自己的想法,所以就粗糙着写了,只是探讨而已哈!
#4
smile康师傅2013-01-08 21:48
//De Co+Live
#include <iostream>
using namespace std;
#include <string>
#include <stdlib.h>
static sample* lastCreated=NULL;//最后一个被创建的对象的地址 -------应该设置为static外部指针就行了。。。
class sample
{
    private:
    sample* front;//指向逻辑上前一个对象的指针
    sample* next;//指向逻辑上后一个对象的指针
    static int iCounter;//计数器 - 有多少个同类对象被创建
    int iSerial;//本对象在第几个
    public:
    sample();
    friend ostream& operator<<(ostream&,sample&);//重载<<运算符   
};
//
int sample::iCounter=0;//初始化iCounter为0
sample::sample()
{
    iCounter++;
    if(iCounter==1)
    {
        front=NULL;
        lastCreated=this;
        next=NULL;
        iSerial=1;   
    }
    else
    {
        front=lastCreated;
        lastCreated->next=this;
        lastCreated=this;
        next=0;
        iSerial=iCounter;
    }
}
//
ostream& operator<<(ostream& OUT,sample& SAMPLE)
{
    OUT<<"本身第"<<SAMPLE.iSerial<<endl;
    OUT<<"总共"<<SAMPLE.iCounter<<endl;
    return OUT;//掉了返回值
}
//****************************************
//
//main()开始
//
//****************************************
int main()
{
    sample sample1;
    cout<<sample1<<endl;
    sample sample2;
    cout<<sample2<<endl;
    system("pause");
    return 0;
}
#5
coleev2013-01-10 13:23
回复 4楼 smile康师傅
3Q. 问题已解决 - 但不是您说的return OUT的问题
#6
coleev2013-01-10 13:26
程序代码:

//De Co+Live
#include <iostream>
using namespace std;
#include <string>
#include <stdlib.h>
//
class sample
{
    private:
    sample* front;   
    sample* next;
    static int iCounter;
    int iSerial;
    string strName;
    public:
    static sample* lastCreated;
    sample();
    friend ostream& operator<<(ostream&,sample&);
    friend void ShowJump(sample*,int);
};
//
//****************************************
//
//定义一个友元函数 - 实现递归sample类指针
//以达到循环输出的目的
//
//****************************************
void ShowJump(sample* ps,int i)
{
    sample* tmp=ps;
    if(ps->iSerial+i>sample::iCounter||i==0)
    {
        //doing nothing
    }
    else
    {
        i--;
        ps=tmp->next;
        cout<<ps->strName<<endl;
        ShowJump(ps,i);        
    }
}
//
int sample::iCounter=0;
sample* sample::lastCreated=NULL;
sample::sample()
{
    iCounter++;
    if(iCounter==1)
    {
        front=NULL;
        lastCreated=this;
        next=NULL;
        iSerial=1;
        cout<<"输入第"<<iSerial<<"者的姓名>>";
        getline(cin,this->strName);
    }
    else
    {
        front=lastCreated;
        ((sample*)lastCreated)->next=this;
        lastCreated=this;
        next=0;
        iSerial=iCounter;
        cout<<"输入第"<<iSerial<<"者的姓名>>";
        getline(cin,this->strName);
    }
}
//
ostream& operator<<(ostream& OUT,sample& SAMPLE)
{
    OUT<<"本身第"<<SAMPLE.iSerial<<endl;
    OUT<<"总共"<<SAMPLE.iCounter<<endl;
    return OUT;
}
//****************************************
//
//main()开始
//
//****************************************
int main()
{
    sample s1,s2,s3,s4,s5,s6,s7,s8;
    cout<<s1<<endl;
    cout<<s2<<endl;
    cout<<s3<<endl;
    cout<<s4<<endl;
    cout<<s4<<endl;
    cout<<s5<<endl;
    cout<<s6<<endl;
    cout<<s7<<endl;
    cout<<s8<<endl;
    ShowJump(&s1,6);
    system("pause");
    return 0;
}


//*********************************************************************
还是把修改后的烂码贴出来吧
1