-7
class mail
{

void reg();
.
.
};

void mail::reg()
{

filout.write((char *)&obj,sizeof(obj)); // error occurring on compilation. undefined sysmbol obj

}

int main()
{


{  //local block

mail obj;
.
.
obj.reg();

} //end of local block


}// end of main

我该如何纠正这个错误。当我调用 memeber 函数时,对象是否被破坏。我的理解是当定义它的块结束时 obj 被销毁

如何将 obj 写入 reg() 中的文件

谢谢

根据要求的完整代码:

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>

int user;           // to keep a track of no. of users

class mail
{
public:
    char un[25];             // user name
    char pd[25];             // passsword
    // no. of inbox messages
    int ino;                 // no. of sent messages
    int sno;
    char isbj[25][25];
    char ssbj[25][25];
    char imsg[25][50];
    char smsg[25][50];       // character array to store inbox and sent messages
    void reg();             // func to sign up
    void signin();
    mail()
    {}
    ~mail()
    {
        cout<<"DESTRUCTER AT WORK\N";
        getch();
    }         // menu available after sign in
};          // object for 10 users

void mail::reg()
{
    clrscr();
    cout<<"Enter user name ( enter unique name )\n";
    gets(un);
    cout<<"Enter password\n";
    gets(pd);
    ofstream filout;
    filout.open("email",ios::app||ios::binary);
    if(!filout)
    {
        cout<<"cannot open file\n";
    }
    else
    {
        ino=-1;
        sno=-1;

        filout.write((char *)&obj,sizeof(obj));
        filout.close();


    }


    cout<<"You are now registered. Plz login\n";
    getch();

}        // end of sign up or register func



void mail::signin()
{
    int f,i;
    char subj[25],message[50],usrnm[25];       // temporary varibles to store

start:
    clrscr();
    cout<<"1.compose \n2.inbox \n3.Sent\n4.logout";
    cout<<"\nEnter ur choice\n";

    int ch;
    cin>>ch;

    switch(ch)
    {

    case 1:
        cout<<"enter username\n";
        gets(usrnm);

        {            // beginning of local loop
            mail temp;

            ifstream filein;
            filein.open("email",ios::in|ios::binary);
            if(!filein)
            {
                cout<<"Unable to open file to read\n";
            }
            else
            {

                filein.read((char *)&temp,sizeof(temp));
                while(filein)
                {
                    if(strcmp(usrnm,temp.un)==0)
                    {

                        cout<<" User name valid\n";


                        f=1;


                        cout<<"Enter subject\n";
                        gets(subj);
                        cout<<"Enter message\n";
                        gets(message);
                        temp.ino++;
                        strcpy(temp.isbj[temp.ino],subj);

                        strcpy(temp.imsg[temp.ino],message);  // copying message to recepient inbox

                        sno++;
                        strcpy(ssbj[sno],subj);
                        strcpy(smsg[sno],message);  // copying messages to sender sent folder

                        cout<<"Your message has been sent successfully\n";
                        break;
                    }
                    else
                    {
                    }


                    filein.read((char *)&temp,sizeof(temp));
                }               // end of while

            }
            filein.close();


            mail temp1;

            long pos;
            fstream fio("email",ios::in|ios::out|ios::binary);
            pos=fio.tellg();
            fio.read((char*)&temp1,sizeof(temp1));

            while(fio)
            {

                if(strcmp(un,temp1.un)==0)
                {

                    fio.seekg(pos);

                    fio.write((char*)&obj,sizeof(obj));
                    break;
                }
                pos=fio.tellg();
                fio.read((char*)&temp1,sizeof(temp1));
            }
            fio.close();

            mail temp2;


            fio.open("email",ios::in|ios::out|ios::binary);
            pos=fio.tellg();
            fio.read((char*)&temp2,sizeof(temp2));

            while(fio)
            {

                if(strcmp(temp.un,temp2.un)==0)
                {

                    fio.seekg(pos);

                    fio.write((char*)&temp,sizeof(temp));
                    break;
                }
                pos=fio.tellg();
                fio.read((char*)&temp2,sizeof(temp2));
            }
            fio.close();

        } // end of block



        if(f==0)
            cout<<"invalid user. Please enter a valid user name\n";
        getch();
        break;

    case 2:
        if(ino<0)
            cout<<"There are no messages in your inbox\n";
        else
        {
            for(i=0;i<=ino;i++)
            {
                cout<<"\nMessage "<<i+1<<"\n";   // printing inbox messages

                puts(isbj[i]);

                puts(imsg[i]);


            }
        }
        getch();
        break;

    case 3:
        if(sno<0)
            cout<<"There are no sent messages\n";
        else
        {
            for(i=0;i<=sno;i++)
            {

                cout<<"\nMessage "<<i+1<<"\n";
                puts(ssbj[i]);
                puts(smsg[i]);

            }
        }
        getch();
        break;

    case 4:
        cout<<"Logout sucess\n";
        return;


    default : cout<<"wrong choice\n";

    } // end of switch

    goto start;

} // end of signin




void screen(void);
void signup(void);


void main()
{
    clrscr();
    char usrnm[25],pwd[25],s[25],c;
    int i;
m:

    clrscr();
    screen();
    gotoxy(30,4);
    c=getch();

    if(c=='i'||c=='I')               //if user opts to sign in
    {

        gotoxy(62,11);
        gets(usrnm);
        gotoxy(62,13);


        for(i=0;i< 25;i++)                   // getting password
        {
            if( i < 0)
                i=0;
            pwd[i]=getch();
            if(pwd[i]=='\b')
            { i-=2;
            cout<<"\b \b";
            continue;
            }

            if(pwd[i]=='\r')
                break;
            cout<<"*";
        }
        pwd[i]='\0';
        clrscr();

        int f;
        {                       // local block

            mail obj;


            ifstream filein;
            filein.open("email",ios::in|ios::binary);
            if(!filein)
            {
                cout<<"Unable to open file to read\n";
            }
            else
            {
                filein.seekg(0);

                filein.read((char *)&obj,sizeof(obj));
                while(filein)
                {
                    if(strcmp(usrnm,obj.un)==0)
                    {
                        f=1;
                        if(strcmp(pwd,obj.pd)==0)
                        {

                            obj.signin();
                            break;
                        }
                        else
                        {
                            cout<<"wrong password\n";
                            getch();

                            break;
                        }


                    } // end of if
                    else
                    {
                    }
                    filein.read((char *)&obj,sizeof(obj));
                }               // end of while


                filein.close();
            } // end of else

        }


        if(f==0)
            cout<<"Wrong username or password\n";
        getch();
        goto m;
    }
    else if(c=='s'||c=='S')       // user opts to sign up
    {
        signup();
        getch();
        goto m;
    }
    else if(c=='e'||c=='E')       // user choose to exit
        exit(0);
    else
    {
        clrscr();
        cout<<"you have entered a wrong key. please check and try again\n";

        getch();
        goto m;
    }

}  //end of main


void signup()                   // sign up function
{



    int t;
    clrscr();
    {
        mail obj;
        ifstream filin;
        filin.open("user.txt",ios::ate);
        if(!filin)
        {
            cout<<"cannot open file\n";
        }
        else
        {
            filin.seekg(0);
            filin>>t;
        }

        filin.close();

        user=t;
        ++user;                     // incrementing no. users

        ofstream filout;
        filout.open("user.txt",ios::out);
        if(!filout)
        {
            cout<<"cannot open file\n";
        }
        else
        {
            filout.seekp(0);
            filout<<user;
        }

        filin.close();

        obj.reg();

    }  // end of block

    return;

}       // end of sign up



void screen()                // function for login screen
{
    int i;
    for(i=0;i<80;i++)
        cout<<"*";
    cout<<" SuperMail                             New to SuperMial ? Create an Account     ";
    for(i=0;i<80;i++)
        cout<<"*";
    cout<<" Press S To Sign up                    Already have a account Press I to sign in";
    for(i=0;i<80;i++)
        cout<<"-";
    gotoxy(1,6);
    cout<<"\n\n\n\n Super mail is built on the idea that \n it can be more intuitive,efficient and \n useful and may be even fun:\n\n Lots of space\n\n Less spam";
    gotoxy(1,22);
    for(i=0;i<80;i++)
        cout<<"-";
    gotoxy(32,23);
    cout<<"PRESS E TO EXIT";
    gotoxy(1,24);
    for(i=0;i<80;i++)
        cout<<"-";
    gotoxy(50,10);
    cout<<"-------------------------------";
    gotoxy(50,11);
    cout<<"| USERNAME :                  |";
    gotoxy(50,12);
    cout<<"-------------------------------";
    gotoxy(50,13);
    cout<<"| PASSWORD :                  |";
    gotoxy(50,14);
    cout<<"-------------------------------";
}

还有其他错误也谢谢

4

4 回答 4

2

该对象没有被破坏,但名称obj在内部不可用reg()- 这正是您在内部命名对象的名称main()

您可以通过 引用成员函数中的当前对象*this,但您也可以直接访问成员。

于 2012-12-19T11:56:21.570 回答
0

您应该objreg()方法中使用this(例如this->data)指针作为reg()邮件类中的成员引用,它obj是一个实例。

于 2012-12-19T11:57:23.720 回答
0

Obj 只是变量的本地名称。当您在类函数中时,您可以使用 this 访问“obj”(这是类的当前实例)。关键字this是指向类实例的指针。你可以做:

this->foo();  //  called a smart pointer
//or
*this.foo();  //  deference the variable
于 2012-12-19T11:58:30.367 回答
0

obj只是一个变量名。您不想在文件中写入 obj 。您想要的是在文件中写入当前的类变量。只需在方法中使用成员变量,因为它们将可用。
或者,如果您有类似这样的外部方法

无效写(邮件* a)
,那么你需要把它称为
写(这个)。
如果写入对象的方法是成员函数,如
无效邮件::写()
,然后简单地调用 write() 并且第一点适用于方法 write。

编辑: - 你不能这样做

filout.write((char *)&obj,sizeof(obj));
. 这将打印出一些乱码。而不是这个尝试打印单个变量的内容。

于 2012-12-19T12:02:26.280 回答