copy constructor question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

copy constructor question

 
0
  #1
Jun 12th, 2008
I have a "two stage" inheritance piece I'm writing. The overall class is telnumber, the worknumber is derived from telnumber, and billnumber is derived from worknumber. The problem is in in my billnumber.cpp. In the main there are two lines referring to the billnumber class with only 4 parameters, and one with 5 parameters. The one with 5 parameters compiles fine, but the two lines with 4 parameters give me the error message "No overloaded function takes 4 arguments. Below is my billnumber cpp. How do I add in a copy constructor for 4 arguments as well as 5?


  1. #include "billnumber class.h"
  2.  
  3. billnumber::billnumber():
  4. worknumber("","","","")
  5. {
  6. }
  7.  
  8. billnumber::billnumber(string i_npa, string i_nxx, string i_line, string i_name, int i_numlines):
  9. worknumber(i_npa, i_nxx, i_line,i_name)
  10. {
  11. }
  12.  
  13. void billnumber::setnumlines(int& newnumlines)
  14. {
  15. numlines=newnumlines;
  16. }
  17.  
  18. int billnumber::getnumlines()
  19. {
  20. return (numlines);
  21. }
  22.  
  23. billnumber::~billnumber()
  24. {
  25. }

Here is the main:
  1. ostream& operator << (ostream &out, telnumber &tn)
  2. {
  3. tn.printtostream(out);
  4. return out;
  5. }
  6.  
  7. int main()
  8. {
  9. telnumber yournumber;
  10. telnumber paul("719","590","6768");
  11. telnumber bob("719","590", "6729");
  12. worknumber csstaff1 ("719","590","6732","Book Store");
  13. worknumber csstaff2 ("212","371","6940","Borland C++ Guru");
  14. worknumber csstaff3 ("405","612","3433","Visual C++ Expert");
  15. billnumber csdept ("719","590","6850","Dean of CS");
  16. billnumber library ("719","598","6708","Librarian");
  17. billnumber reception ("719","598","0200","Receptionist",35);
  18. cout << "Testing the overladen << operator with the virtual" << "printtostream()\n\n";
  19. cout << "The telephone numbers are: \n" << endl;
  20. cout << yournumber << endl;
  21. cout << paul << endl;
  22. cout << bob << endl;
  23. cout << "The working telephone numbers are: \n" << endl;
  24. cout << csstaff1 << endl;
  25. cout << csstaff2 << endl;
  26. cout << csstaff3 << endl;
  27. cout << "The billing telephone numbers are: \n" << endl;
  28. cout << csdept << endl;
  29. cout << library << endl;
  30. cout << reception << endl;
  31. cout << "Here endeth the hierarchy of the telephone!" << endl;
  32.  
  33. return 0;
  34. }
Last edited by henpecked1; Jun 12th, 2008 at 11:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: copy constructor question

 
0
  #2
Jun 12th, 2008
I'm also getting three unresolved externals from the "print" function above main. It is declared as a virtual in the telnumber header and there is no implementation. I know I've been told about external errors before, but I can't seem to see where I've muffed this one thanks to the inheritance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 2
Reputation: al_crow is an unknown quantity at this point 
Solved Threads: 1
al_crow al_crow is offline Offline
Newbie Poster

Re: copy constructor question

 
0
  #3
Jun 12th, 2008
where is the 4 parameters constructor? What I can see is just a 5 parameters constructor and a default constructor.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: copy constructor question

 
0
  #4
Jun 12th, 2008
Originally Posted by al_crow View Post
where is the 4 parameters constructor? What I can see is just a 5 parameters constructor and a default constructor.

That's just it...I have tried to write the 4 parameter constructor, but I'm doing something wrong, it tells me I have a duplicate body or that I'm redefining among other things. I apparently don't know how to write it. My first attempt was to do it by just eliminating the fifth parameter and placing it above the constructor with 5 parameters...that was a no go. I then did the same, but took out the "worknumber" parameters and that didn't work either.

This is what I tried first:
  1. billnumber::billnumber(string i_npa, string i_nxx, string i_line, string i_name):
  2. worknumber(i_npa, i_nxx, i_line,i_name)
  3. {
  4. }
  5.  
  6. // tried this second
  7.  
  8. billnumber::billnumber(string i_npa, string i_nxx, string i_line, string i_name):
  9. {
  10. }
Obviously neither works. What am I doing wrong? And can you tell me about the external errors above as well?
Last edited by henpecked1; Jun 12th, 2008 at 11:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: copy constructor question

 
0
  #5
Jun 13th, 2008
Al was either of those even close to what you were going to suggest?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 2
Reputation: al_crow is an unknown quantity at this point 
Solved Threads: 1
al_crow al_crow is offline Offline
Newbie Poster

Re: copy constructor question

 
0
  #6
Jun 13th, 2008
Firstly, they are not copy constructot, just constructor.
Do you have declear a 4 parameters constructor in your head file?

I have no clue about the external issue
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: copy constructor question

 
0
  #7
Jun 13th, 2008
yes...the hierarchy works like this

telnum->worknum->billnum

There are three parameters in telnum declared and defined

Those three parameters are passed to worknum, which adds one (name)

Those 4 parameters are then passed to billnum, which adds a fifth (numlines).

At this point, all constructors work fine except that I need one in billnum which will hold the 4 parameters from telnum and worknum and all my attempts at that have failed miserably.

My other problem is with the ostream function right before main. It gives me three extern errors, one for each class, so I've defined something wrong somewhere for that also
Last edited by henpecked1; Jun 13th, 2008 at 1:53 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 979
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 209
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: copy constructor question

 
1
  #8
Jun 13th, 2008
Two simple examples, hopefully you'll get something figured out of those ...
  1. class a
  2. {
  3. public:
  4. a(string s1, string s2){}
  5. };
  6. class b : public a
  7. {
  8. public:
  9. // default ctor
  10. b() : a("", "") {}
  11.  
  12. // ctor with 4 strings
  13. b(string as1, string as2, string bs1, string bs2) : a(as1, as2) {}
  14.  
  15. // ctor with 4 strings plus an int
  16. b(string as1, string as2, string bs1, string bs2, int bint) : a(as1, as2) {}
  17. };
  18. int main()
  19. {
  20. b b1("asdf", "asdf", "asdf", "asdf");
  21. b b2("asdf", "asdf", "asdf", "asdf", 123);
  22.  
  23. return 0;
  24. }
The above changed so, that the 5 arg ctor has a default value for the integer argument, that way the 4 arg ctor can be dropped altogether ...
  1. class a
  2. {
  3. public:
  4. a(string s1, string s2){}
  5. };
  6. class b : public a
  7. {
  8. public:
  9. // default ctor
  10. b() : a("", "") {}
  11.  
  12. // ctor with 4 strings plus an int with default value zero
  13. b(string as1, string as2, string bs1, string bs2, int bint = 0) : a(as1, as2) {}
  14. };
  15. int main()
  16. {
  17. b b1("asdf", "asdf", "asdf", "asdf"); // this one uses the default value
  18. b b2("asdf", "asdf", "asdf", "asdf", 123);
  19.  
  20. return 0;
  21. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: copy constructor question

 
0
  #9
Jun 13th, 2008
Thank you mitrim. Using your example, I was able to figure out the 4 vs 5 parameter problem.

If someone could look through the previous posts on this subject, I have this snippet as a virtual function in each of the classes
  1. virtual void printtostream(ostream& out);
Since it is a virtual function it was not defined in any of the classes. My function, just slightly pre main is this
  1. ostream& operator << (ostream &out, telnumber &tn)
  2. {
  3. tn.printtostream(out);
  4. return out;
  5. }

This gives me three external resolution errors. What am I forgetting to do?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: copy constructor question

 
0
  #10
Jun 13th, 2008
nm, I'll post this separately..thanks again mitrim
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC