2012-12-19 18 views
-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<<"-------------------------------"; 
} 

還有其他的錯誤也 感謝

+2

真實的代碼和真正的錯誤信息呢? – PlasmaHH

+0

它真的很長。你不會去讀它。所以我縮短了它 –

+0

讓我們決定是否要閱讀它。總是發佈完整的錯誤消息! – Pubby

回答

2

對象不被破壞,BU t名稱objreg()內部不可用 - 這就是您在main()內命名您的對象的內容。

您可以通過*this來引用成員函數中的當前對象,但也可以直接訪問成員。

+0

所以代碼將是filout.write((char *)&obj,sizeof(* this));對 ? –

+1

@mohdrafi'&obj'?或者'這個'? –

0

你應該參考obj使用thisreg()方法(例如這個 - >數據)指針reg()是在郵件類的成員,其obj是的一個實例。

0

Obj只是變量的本地名稱。當你在類函數中時,你可以通過使用這個來訪問'obj'(這是類的當前實例)。關鍵字this是一個指向類實例的指針。你可以這樣做:

this->foo(); // called a smart pointer 
//or 
*this.foo(); // deference the variable 
0

obj是只是一個變量名。你不想在文件中寫入obj。你想要的是在文件中寫入當前類變量。只需在方法中使用成員變量即可使用。
或者,如果你有外部的方法是這樣的

 void write(mail* a)
,那麼你需要調用它作爲
write(this).
如果其寫入對象的方法就像是
void mail::write()
成員函數,然後簡單地調用write()和第一點適用於方法寫。編輯: - 你不能這樣做
 filout.write((char *)&obj,sizeof(obj));
。這會打印出一些亂碼。而不是嘗試打印個別變量的內容。

+0

我想將obj的全部內容寫入文件。 Iam不確定如何單獨編寫成員以及是否影響其餘代碼 –

相關問題