Wow I totally missed that this thread went to the second page!
First of all, thanks everyone for the comments.
@Elysia, I decided to remove some of the codes that my teacher had in his driver.cpp file in order to make things clearer for myself(and you guys) to find out where the problem is from...I also removed some of the functions defined in header and implementation. It was just so messy...
@laserlight: sorry if I missed your note, I read it, but I didn't know what you meants by getFOO() lol, but now I know...sure I'll be more clear about what I'm doing from now on
here is the original driver file:
Code:
int main( ) {
SodaCan myPepsi;
SodaCan yourDietCoke;
SodaCan bigGulp;
SodaCan junk;
bigGulp.setBrand( "Pepsi" );
bigGulp.setSize( 128 );
myPepsi.setBrand( "Pepsi" );
myPepsi.setSize( 12 );
yourDietCoke.setBrand( "Coke" );
yourDietCoke.setSize( 12 );
myPepsi.pourInSoda( 2 );
yourDietCoke.pourInSoda( 3 );
bigGulp.pourInSoda( 64 );
/// junk should have 76 ounces
junk = bigGulp + myPepsi;
cout << junk;
if (junk > bigGulp) {
cout <<"junk’s bigger"<< endl;
}
if (junk >= bigGulp) {
cout <<"junk’s bigger or equal"<< endl;
}
if (myPepsi < junk) {
cout<<"myPepsi’s smaller"<<endl;
}
if (myPepsi <= junk) {
cout<<"myPepsi’s smaller or equal"<<endl;
}
if (myPepsi != junk) {
cout <<"myPepsi != junk!"<<endl;
}
if (myPepsi == myPepsi) {
cout <<"equal test works"<<endl;
}
SodaCan * ptrCan = new SodaCan( "Coke", 25, 3 );
SodaCan * nullCan = NULL;
// be careful
cout << nullCan << endl;
cout << ptrCan << endl;
cin >> ptrCan;
// notice the difference
cout << ptrCan << endl;
ptrCan->pourInSoda( 10 );
/// notice the difference
cout << ptrCan << endl;
delete (ptrCan);
// why can't I do this???
// delete( nullCan );
return( 0 );
}
and here is the new one, after I removed some of the codes I assumed are making problem:
Code:
int main( ) {
SodaCan myPepsi;
SodaCan yourDietCoke;
SodaCan bigGulp;
SodaCan junk;
bigGulp.setBrand( "Pepsi" );
bigGulp.setSize( 128 );
myPepsi.setBrand( "Pepsi" );
myPepsi.setSize( 12 );
yourDietCoke.setBrand( "Coke" );
yourDietCoke.setSize( 12 );
myPepsi.pourInSoda( 2 );
yourDietCoke.pourInSoda( 3 );
bigGulp.pourInSoda( 64 );
/// junk should have 76 ounces
junk = bigGulp + myPepsi;
if (junk > bigGulp) {
cout <<"junk’s bigger"<< endl;
}
if (junk >= bigGulp) {
cout <<"junk’s bigger or equal"<< endl;
}
if (myPepsi < junk) {
cout<<"myPepsi’s smaller"<<endl;
}
if (myPepsi <= junk) {
cout<<"myPepsi’s smaller or equal"<<endl;
}
if (myPepsi != junk) {
cout <<"myPepsi != junk!"<<endl;
}
if (myPepsi == myPepsi) {
cout <<"equal test works"<<endl;
}
SodaCan * ptrCan = new SodaCan( "Coke", 25, 3 );
// be careful
cout << ptrCan << endl;
delete (ptrCan);
// why can't I do this???
// delete( nullCan );
return( 0 );
}
I compiled the latter and there was no problem, it worked nicely
so driver.cpp above works fine.
Also, the definition of my overloaded function was:
Code:
std::ostream& operator <<( std::ostream& outs, const SodaCan *can ){
outs<< can->getContents() <<" ounces of " <<can->getBrand() <<" in a "<<can->getSize()<< " can.";
return outs ;
}
then I added these two lines to it(which was in the original Driver.cpp file by my teacher):
Code:
SodaCan * nullCan = NULL;
cout << nullCan << endl;
the program crashes...
1) how do you suggest I escape this issue?! exception handling? if yes, I need some guidance on the subject, I know a little bit about it, but we have not covered it in class yet.
2) Is it possible to overload << where the arguments are not pointers, while having the overloaded version of >> with pointers as arguments?
I'm also trying to understand the conversation MutantJohn started and the comments followed by it...complicated stuff,god I'm so behind on these topics lol