I hope I'm not being to dense, I'm new to C++.
Background: School project to model a media center. Class MediaCollection holds an array of pointers to objects derived from abstract class Media. The classes derived from Media are AudioCassette, AudioCD, VideoVHS and VideoDVD.
The idea is to create a collection, add CD's, DVD's etc., and simulate playing, fast forwarding, rewinding, etc. We've been given (most of) the header files.
My problem is implementing the pure virtual function "virtual void PrintMedia(ostream & Out) = 0" in the abstract class Media. It's supposed to print the details of one of the media objects (CD or DVD). I implemented as follows in each of the four media type classes:
(AudioCassette.cpp example)
#include <iostream>
using namespace std;
void AudioCassette::PrintMedia(ostream & Out){
Out << "\nMedia Type: Audio Cassette\nTitle: " << Title <<
"\nArtist: " << Artist << "\nWhere Purchased: " << WherePurchased
<< "\nCost: $" << Cost << "\nRunning Time: " << RunningTime <<
"\nNumber of Selections: " << NumberOfSelections << endl;
}
It works, but I have to pass it cout as an argument when I call the function. I think I'm supposed to be passing Out as the argument, but when I try that, I get Out: undeclared identifier.
I have another function "void ListMediaDetails()", in the MediaCollection.cpp that is supposed to call the PrintMedia() function on the media type selected in the array. I implemented that one as follows:
#include "MediaCollection.h"
#include "AudioCassette.h"
#include "AudioCD.h"
#include "VideoVHS.h"
#include "VideoDVD.h"
#include <iostream>
using namespace std;
void MediaCollection::ListMediaDetails(){
int Selection = 0;
ListMediaTitles();
cout <<"\nSelect the number of the item to display: ";
cin >> Selection;
cin.ignore(cin.rdbuf()->in_avail(), '\n');
Collection[Selection -1]->PrintMedia(Out);
}
This one gives the same error when I use "Out" as the argument. If I try to use "cout" as the argument I get - "binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)".
I hope I have included enough, but not to much info. I would appreciate any help anyone could offer. Thanks.
You are supposed to be passing an ostream as an argument. If you want to print to the screen, you would pass std::cout. Out is just the name of the argument.
In #2, you are not actually the calling the function in the way you think you are doing. You are instead attempting to print out the return value of the PrintMedia(), which is not returning anything (returns void).
The function will take care of printing the stuff, you just call it like any other function.