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<<"-------------------------------";
}
还有其他错误 感谢
答案 0 :(得分:2)
对象未被销毁,但obj
内的名称reg()
不可用 - 这正是您在main()
内命名对象的原因。
您可以通过*this
引用成员函数中的当前对象,但您也可以直接访问成员。
答案 1 :(得分:0)
您应该使用obj
方法reg()
引用this
(例如this-&gt; data)指针,因为reg()
是邮件类中的成员{{1}是。的实例。
答案 2 :(得分:0)
Obj只是变量的本地名称。当你在类函数中时,可以使用它来访问'obj'(这是该类的当前实例)。关键字this
是指向类实例的指针。你可以这样做:
this->foo(); // called a smart pointer
//or
*this.foo(); // deference the variable
答案 3 :(得分:0)
obj 只是一个变量名称。你不想在文件中写obj。你想要的是在文件中写出当前的类变量。只需使用方法中的成员变量即可。
或者,如果你有类似这样的外部方法
void write(mail* a),那么你需要将其称为
write(this).如果写入对象的方法是
void mail::write()之类的成员函数,那么只需调用write()并将第一个点应用于方法write。
编辑: - 你无法做到
filout.write((char *)&obj,sizeof(obj));。这将打印出一些乱码。而不是尝试打印单个变量的内容。