| | |
copy constructor question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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?
Here is the main:
C++ Syntax (Toggle Plain Text)
#include "billnumber class.h" billnumber::billnumber(): worknumber("","","","") { } billnumber::billnumber(string i_npa, string i_nxx, string i_line, string i_name, int i_numlines): worknumber(i_npa, i_nxx, i_line,i_name) { } void billnumber::setnumlines(int& newnumlines) { numlines=newnumlines; } int billnumber::getnumlines() { return (numlines); } billnumber::~billnumber() { }
Here is the main:
C++ Syntax (Toggle Plain Text)
ostream& operator << (ostream &out, telnumber &tn) { tn.printtostream(out); return out; } int main() { telnumber yournumber; telnumber paul("719","590","6768"); telnumber bob("719","590", "6729"); worknumber csstaff1 ("719","590","6732","Book Store"); worknumber csstaff2 ("212","371","6940","Borland C++ Guru"); worknumber csstaff3 ("405","612","3433","Visual C++ Expert"); billnumber csdept ("719","590","6850","Dean of CS"); billnumber library ("719","598","6708","Librarian"); billnumber reception ("719","598","0200","Receptionist",35); cout << "Testing the overladen << operator with the virtual" << "printtostream()\n\n"; cout << "The telephone numbers are: \n" << endl; cout << yournumber << endl; cout << paul << endl; cout << bob << endl; cout << "The working telephone numbers are: \n" << endl; cout << csstaff1 << endl; cout << csstaff2 << endl; cout << csstaff3 << endl; cout << "The billing telephone numbers are: \n" << endl; cout << csdept << endl; cout << library << endl; cout << reception << endl; cout << "Here endeth the hierarchy of the telephone!" << endl; return 0; }
Last edited by henpecked1; Jun 12th, 2008 at 11:14 pm.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
billnumber::billnumber(string i_npa, string i_nxx, string i_line, string i_name): worknumber(i_npa, i_nxx, i_line,i_name) { } // tried this second billnumber::billnumber(string i_npa, string i_nxx, string i_line, string i_name): { }
Last edited by henpecked1; Jun 12th, 2008 at 11:41 pm.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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
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.
•
•
Join Date: Nov 2007
Posts: 979
Reputation:
Solved Threads: 209
Two simple examples, hopefully you'll get something figured out of those ...
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 ...
C++ Syntax (Toggle Plain Text)
class a { public: a(string s1, string s2){} }; class b : public a { public: // default ctor b() : a("", "") {} // ctor with 4 strings b(string as1, string as2, string bs1, string bs2) : a(as1, as2) {} // ctor with 4 strings plus an int b(string as1, string as2, string bs1, string bs2, int bint) : a(as1, as2) {} }; int main() { b b1("asdf", "asdf", "asdf", "asdf"); b b2("asdf", "asdf", "asdf", "asdf", 123); return 0; }
C++ Syntax (Toggle Plain Text)
class a { public: a(string s1, string s2){} }; class b : public a { public: // default ctor b() : a("", "") {} // ctor with 4 strings plus an int with default value zero b(string as1, string as2, string bs1, string bs2, int bint = 0) : a(as1, as2) {} }; int main() { b b1("asdf", "asdf", "asdf", "asdf"); // this one uses the default value b b2("asdf", "asdf", "asdf", "asdf", 123); return 0; }
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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
Since it is a virtual function it was not defined in any of the classes. My function, just slightly pre main is this
This gives me three external resolution errors. What am I forgetting to do?
If someone could look through the previous posts on this subject, I have this snippet as a virtual function in each of the classes
C++ Syntax (Toggle Plain Text)
virtual void printtostream(ostream& out);
C++ Syntax (Toggle Plain Text)
ostream& operator << (ostream &out, telnumber &tn) { tn.printtostream(out); return out; }
This gives me three external resolution errors. What am I forgetting to do?
![]() |
Similar Threads
- Copy constructor in derived class (C)
- overloaded operators question (C)
- This Pointer / Copy Constructor (C++)
- copy constructor and 2 args constructor help (C)
- simple structure question (C++)
- (C++) Writing a Copy Constructor?!? (C++)
- copy constructor problem (C++)
- A little Help (C++)
Other Threads in the C++ Forum
- Previous Thread: assess database using C++
- Next Thread: Passing Multidimensional Arrays To Functions
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





