Can't find the solution and looking for help

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 9
Reputation: cerb63 is an unknown quantity at this point 
Solved Threads: 0
cerb63 cerb63 is offline Offline
Newbie Poster

Can't find the solution and looking for help

 
0
  #1
Nov 16th, 2008
I'm having an issue that so that Book A and Book B's Author and Title will print at the end of the program. So This will verify that the overloaded = is functioning.

----------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;

class Book
{
public:
Book(char*, char*);
private:
char* title;
char* author;

public:
Book& Book::operator=(const Book& b)
{
if(this != &b)
{
delete [] author;
delete [] title;

author = new char[strlen(b.author)+1];
title = new char[strlen(b.title)+1];
strcpy(author, b.author);
strcpy(title, b.title);
}
return *this;
}

};

int main(int argc, char* argv[])
{
Book* pA = new Book("Aardvark", "Be an Aardvark on pennies a day");
Book* pB = new Book("Speelburgh", "ET vs. Howard the Duck");

pA = pB;

//complete the program so that Book A and Book B's Author and Title print
cout << "The value of Book A's Author and Title are: " << pA.author<<" "<<pA.title<<endl;
cout << "The value of Book B's Author and Title are: " << pB.author<<" "<<pB.title<<endl;

system("pause");
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Can't find the solution and looking for help

 
0
  #2
Nov 16th, 2008
pA and pB are both pointers to objects, not objects per se. Therefore use the arrow operatos to acess the data members, not the dot operator.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 9
Reputation: cerb63 is an unknown quantity at this point 
Solved Threads: 0
cerb63 cerb63 is offline Offline
Newbie Poster

Re: Can't find the solution and looking for help

 
0
  #3
Nov 17th, 2008
I tried that and ended up with a compiler error and I still don't know what I did wrong
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Can't find the solution and looking for help

 
0
  #4
Nov 17th, 2008
Originally Posted by cerb63 View Post
I tried that and ended up with a compiler error and I still don't know what I did wrong
We don't either until you show what you tried. Lerner is correct. You need the arrow operator, not the dot operator. Currently you have the dot operator. Use code tags:


[code]
// paste code here
[/code]
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 9
Reputation: cerb63 is an unknown quantity at this point 
Solved Threads: 0
cerb63 cerb63 is offline Offline
Newbie Poster

Re: Can't find the solution and looking for help

 
0
  #5
Dec 7th, 2008
The issue is with these two snippets of code:_______________________________________________________________________________________________________
cout << "The value of Book A's Author and Title are: " << pA.author<<" "<<pA.title<<endl;
cout << "The value of Book B's Author and Title are: " << pB.author<<" "<<pB.title<<endl;
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Can't find the solution and looking for help

 
0
  #6
Dec 7th, 2008
Originally Posted by cerb63 View Post
The issue is with these two snippets of code:_______________________________________________________________________________________________________
cout << "The value of Book A's Author and Title are: " << pA.author<<" "<<pA.title<<endl;
cout << "The value of Book B's Author and Title are: " << pB.author<<" "<<pB.title<<endl;
Code tags please:

[code]
// paste code here
[/code]


Use the arrow operator as Lerner mentioned:
cout << "The value of Book A's Author and Title are: " << pA->author<<" "<<pA->title<<endl;

Same with the other line. Use the -> operator with pointers.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 9
Reputation: cerb63 is an unknown quantity at this point 
Solved Threads: 0
cerb63 cerb63 is offline Offline
Newbie Poster

Re: Can't find the solution and looking for help

 
0
  #7
Dec 11th, 2008
that worked but I still am having an issue with the a compiling error
for char* author;
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Can't find the solution and looking for help

 
0
  #8
Dec 11th, 2008
new Book("Aardvark", "Be an Aardvark on pennies a day");

That syntax requires definition (which I don't see in the code posted) as well as declaraton (which you arlready have) of a nondefault constructor taking two char * as arguments. It will probably look a fair amount like the code for the assignment operator thay you have already written.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 10
Reputation: tarakant_sethy is an unknown quantity at this point 
Solved Threads: 0
tarakant_sethy tarakant_sethy is offline Offline
Newbie Poster

Re: Can't find the solution and looking for help

 
0
  #9
Dec 12th, 2008
the program has many pros,
1. constructor has no definition.
2. in the cout u r trying to access the private variables directly

write a contructor and a public function to access display the private data
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 505 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC