How use the overload of>> (or<<) of a class in the overload of << and >> of another class?

P

Piotre Ugrumov

I have done the overload on the operator >> and << in the class Attore.
These 2 overload work correctly.
I have done the overload of the same overload in the class Film. The class
film ha inside an array of pointer to Attore.
I have written these overload in these ways. the overload of << work
correctly the overload of >> I don't know. I compile the class correctly,
when I insert a film through the operator >> I don't receive error, but when
I try to stamp the fil through the operator << I have an error during the
execution of program. Why this error? Depend from the overload of >>.
There are the overload of the operator<< and >>:
ostream & operator<<(ostream &out, const Film &f){
out<<f.getTitolo()<<endl;
out<<f.getAnno()<<endl;
for(int i=0; i<f.countAttori(); i++){
out<<*(f.attori)<<endl; //use the overload of << in Attore, in Film I
have defined: Attore *attori[5]; this overload works correctly
}
return out;
}

istream & operator>>(istream &in, Film &f){
char nome[30];
int anno;
Attore a;
in>>nome;
in>>anno;
in>>a; //I use the overload of >> in Attore;
f.setTitolo(nome);
f.setAnno(anno);
f.addAttore(a);
return in;
}

how can i resolve this problem?
Thanks
 
N

Nick Hounsome

Piotre Ugrumov said:
I have done the overload on the operator >> and << in the class Attore.
These 2 overload work correctly.
I have done the overload of the same overload in the class Film. The class
film ha inside an array of pointer to Attore.
I have written these overload in these ways. the overload of << work
correctly the overload of >> I don't know. I compile the class correctly,
when I insert a film through the operator >> I don't receive error, but when
I try to stamp the fil through the operator << I have an error during the
execution of program. Why this error? Depend from the overload of >>.
There are the overload of the operator<< and >>:
ostream & operator<<(ostream &out, const Film &f){
out<<f.getTitolo()<<endl;
out<<f.getAnno()<<endl;
for(int i=0; i<f.countAttori(); i++){

problem with countAttori() perhaps?
out<<*(f.attori)<<endl; //use the overload of << in Attore, in Film I
have defined: Attore *attori[5]; this overload works correctly
}
return out;
}

istream & operator>>(istream &in, Film &f){
char nome[30];
int anno;
Attore a;
in>>nome;


This could overrun and will only get one word. Use a std::string.
in>>anno;

You are not checking for success at all.
in>>a; //I use the overload of >> in Attore;

You can have 5 actors but the input routine only allows 1.
f.setTitolo(nome);
f.setAnno(anno);
f.addAttore(a);

does what to countAttori() ?
 
P

Piotre Ugrumov

problem with countAttori() perhaps?



countAttori() returns the number of Attore in the array. The max number is
5, but in the array could be less of 5.
I insert with the operator only 1 Attore.
In the overload of >> in the film I have written:
char title[30];
in>>title;
In this way I can give a title of only one word, with the use of string Can
I give a title with 2 or more word?
For insert a film I write this in the prompt:
Goffy(title) 1990(production year) Jacques(actor name) Rosseau(actor
surname) 11/11/1770(actor date of birth)
but hao can a give a title with 2 or more word?
Example:
Goffy go to disco(title) 1990(production year) Jacques(actor name)
Rosseau(actor surname) 11/11/1770(actor date of birth)
Thanks.
 
N

Nick Hounsome

Piotre Ugrumov said:
countAttori() returns the number of Attore in the array. The max number is
5, but in the array could be less of 5.

I gathered that but where is the current size held and are you setting it on
I insert with the operator only 1 Attore.
In the overload of >> in the film I have written:
char title[30];
in>>title;
In this way I can give a title of only one word, with the use of string Can
I give a title with 2 or more word?

No - the only simple way is to allow a whole line
for the title (and possibly every string field) and use getline to read it
(preferably into a std::string).
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top