Error with overload of >> and << but not only :-(((((

P

Piotre Ugrumov

I'm following your help. I have written the overload of the operator <<.
This overload work! :)
But I have some problem with the overload of the operator >>. I have written
the overload of this least operator for the class Person, but I don't know
how write the overload for a class that derived from the class Person.
The overload of << in Person is this:
ostream & operator<<(ostream &out, const Persona &p){
out<<p.getNome()<<" "<<p.getCognome()<<", "<<p.getDataNascita()<<endl;
return out;
}
The overload of << in class Client: Public Person:
ostream & operator<<(ostream &out, const Cliente &p){
out<<"Cliente: "<<endl;
out<<(Persona)p;
out<<"Credito: "<<p.getCredito()<<endl;
return out;
}

The overload of >> in class Person is:
istream & operator>>(istream &in, Persona &p){
char tmp1[30];
char tmp2[30];
char tmp3[30];
in>>tmp1;
in>>tmp2;
in>>tmp3;
p.setName(tmp1);
p.setSurname(tmp2);
p.setDateOfBirth(tmp3);
return in;
}
but how can I implement the overload of >> in the class Client?
?????

Another thing about the overload of>>.
If I create a new class and in this class I insert object of the class
Client and of the class Tradar(derives from Person) how can implemente the
overload of >>?
The overload of << is ok, but the overload of >>? How can I implement this
overload?

In a class I have defined:
Client *cPtr;
Film *fPtr;
Actor *aPtr;
int csize, fsize, asize;

I inizilize all these element to 0.
For add an object I have created this function:
void Archiveo::addClient(Client &c){
Client *tmp=cPtr;
csize = csize+1;
cPtr=new Cliente[csize];
for(int i=0; i<csize; i++)
cPtr=tmp;
cPtr[csize-1]=c;
delete[] tmp;
}
The compiler don't give me any error, but when I execute a main with an
instruction like this: a.addClient(a); all is blocked.
How can resolve this problem?
Thanks.
 
K

Karl Heinz Buchegger

Piotre said:
I'm following your help. I have written the overload of the operator <<.
This overload work! :)
But I have some problem with the overload of the operator >>. I have written
the overload of this least operator for the class Person, but I don't know
how write the overload for a class that derived from the class Person.
The overload of << in Person is this:
ostream & operator<<(ostream &out, const Persona &p){
out<<p.getNome()<<" "<<p.getCognome()<<", "<<p.getDataNascita()<<endl;
return out;
}
The overload of << in class Client: Public Person:
ostream & operator<<(ostream &out, const Cliente &p){
out<<"Cliente: "<<endl;
out<<(Persona)p;
out<<"Credito: "<<p.getCredito()<<endl;
return out;
}

The overload of >> in class Person is:
istream & operator>>(istream &in, Persona &p){
char tmp1[30];
char tmp2[30];
char tmp3[30];
in>>tmp1;
in>>tmp2;
in>>tmp3;
p.setName(tmp1);
p.setSurname(tmp2);
p.setDateOfBirth(tmp3);
return in;
}
but how can I implement the overload of >> in the class Client?
?????

In principle you do the same you did in op>> for class Person:
You define how the user should do his input (what in which order)
and program it that way. Of course you can use op>> for class Person
to simplify your work.
Eg. you could define: If a user needs to enter a Client, he has to
provide (in that order): Name, Surname, Date of Birth, Credit

istream& operator>> ( istream& in, Client& p )
{
// op >> for class Person will read name, surname, date of birth
operator>>( in, (Persona&)p );

int credit;
in >> credit;

// do whatever you need to do with credit

return in;
}
Another thing about the overload of>>.
If I create a new class and in this class I insert object of the class
Client and of the class Tradar(derives from Person) how can implemente the
overload of >>?
The overload of << is ok, but the overload of >>? How can I implement this
overload?

In very much the same way.
You seem to be under the impression that there is some magic about
operators. There is not. Operators are plain vanilla ordinary functions.
The only magical thing about them is the way they are used.

So ask youself: What do I need to do to read in an object of that
type from a stream. If you can do this as ordinary function:

istream& ReadFromStream( istream& in, Tradar& ob )
{
// fill the object ob with data read from the stream in

return in;
}

Now replace the functions name with operator >> and you are done.
In a class I have defined:
Client *cPtr;
Film *fPtr;
Actor *aPtr;
int csize, fsize, asize;

I inizilize all these element to 0.
For add an object I have created this function:
void Archiveo::addClient(Client &c){
Client *tmp=cPtr;
csize = csize+1;
cPtr=new Cliente[csize];
for(int i=0; i<csize; i++)
cPtr=tmp;


csize is already incremented by 1. Thus this
loop tries to access an element from
tmp which doesn't exist.
cPtr[csize-1]=c;
delete[] tmp;
}
The compiler don't give me any error, but when I execute a main with an
instruction like this: a.addClient(a); all is blocked.
How can resolve this problem?

By fireing up your debugger and searching the bug.
Or: By not doing memory management on your own. In the
above a std::vector does all of the management under the
hood for you. You just need to use it. And of course you
can count on std::vector to do it right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top