Derived class question

S

swilks06

I'm trying to create a derived class called DotctorType from a base class called PersonType. I thought I was doing everything correctly, but when I complied the code I had several IntelliSense errors to pop up. I can't see what's causing the errors. Can anyone else see what I'm doing wrong? Here is the code:

01#include <string>
02#include <iostream>
03using namespace std;
04
05class PersonType
06{
07public:
08 // Function to output the first name and the last name.
09 void print() const;
10
11 //Function to set the first name and the last name.
12 void setName(string first, string last);
13
14 //Function to return the first name.
15 string getFirstName() const;
16
17 //Function to return the last name.
18 string getLastName() const;
19
20 // Constructor to set the first name and the last name.
21 PersonType(string first="", string last="");
22
23private:
24 string firstName; // Variable to store the first name.
25 string lastName; // Variable to store the last name.
26};
27
28// Function to output the first name and the last name.
29 void PersonType::print() const
30 {
31 cout<<firstName<< " "<<lastName<<endl;
32 }
33
34 //Function to set the first name and the last name.
35 void PersonType::setName(string first, string last)
36 {
37 firstName = first;
38 lastName = last;
39 }
40
41 //Function to return the first name.
42 string PersonType::getFirstName() const
43 {
44 return firstName;
45 }
46
47 //Function to return the last name.
48 string PersonType::getLastName() const
49 {
50 return lastName;
51 }
52
53 // Constructor to set the first name and the last name.
54 PersonType::personType(string first, string last)
55 {
56 setName(first, last);
57 }
58
59class DotorType : public PersonType
60{
61private:
62 string drtype;
63
64public:
65 string getSpeciality() const;
66 void setSpeciality(string dtype);
67 DoctorType(string first="", string last="", string type="");
68
69};
70
71string DoctorType::getSpeciality();
72{
73 return drtype;
74}
75
76void DoctorType::setSpeciality(string dtype)
77{
78 drtype = dtype;
79}
80
81
82DoctorType::DoctorType(string first="", string last="", string type="")
83 : PersonType(string first="", string last="")
84{
85 setSpeciality(type);
86}






********And here are the errors:
Error 1 error LNK2001: unresolved external symbol _mainCRTStartup`
Error 2 error LNK1120: 1 unresolved externals
3 IntelliSense: explicit type is missing ('int' assumed) (Line 67)
4 IntelliSense: name followed by '::' must be a class or namespace name (Line 71)
5 IntelliSense: expected a declaration (Line 71)
6 IntelliSense: name followed by '::' must be a class or namespace name (Line 76)
7 IntelliSense: identifier "drtype" is undefined (Line 78)
8 IntelliSense: name followed by '::' must be a class or namespace name (Line 82)
9 IntelliSense: expected a '{' (Line 83)
10 IntelliSense: identifier "setSpeciality" is undefined (Line 85)
 
V

Victor Bazarov

I'm trying to create a derived class called DotctorType from a base
class called PersonType. I thought I was doing everything correctly, but
when I complied the code I had several IntelliSense errors to pop up. I
can't see what's causing the errors. Can anyone else see what I'm doing
wrong?

Plenty. First of all, please don't put line numbers when you post code.
They just mess the code up. Post just the code, no numbers.

Second, you've got extraneous semicolons, see below.
Here is the code:

01#include <string>
02#include <iostream>
03using namespace std;
04
05class PersonType
06{
07public:
08 // Function to output the first name and the last name.
09 void print() const;
10
11 //Function to set the first name and the last name.
12 void setName(string first, string last);
13
14 //Function to return the first name.
15 string getFirstName() const;
16
17 //Function to return the last name.
18 string getLastName() const;
19
20 // Constructor to set the first name and the last name.
21 PersonType(string first="", string last="");
22
23private:
24 string firstName; // Variable to store the first name.
25 string lastName; // Variable to store the last name.
26};
27
28// Function to output the first name and the last name.
29 void PersonType::print() const
30 {
31 cout<<firstName<< " "<<lastName<<endl;
32 }
33
34 //Function to set the first name and the last name.
35 void PersonType::setName(string first, string last)
36 {
37 firstName = first;
38 lastName = last;
39 }
40
41 //Function to return the first name.
42 string PersonType::getFirstName() const
43 {
44 return firstName;
45 }
46
47 //Function to return the last name.
48 string PersonType::getLastName() const
49 {
50 return lastName;
51 }
52
53 // Constructor to set the first name and the last name.
54 PersonType::personType(string first, string last)
55 {
56 setName(first, last);
57 }
58
59class DotorType : public PersonType
60{
61private:
62 string drtype;
63
64public:
65 string getSpeciality() const;
66 void setSpeciality(string dtype);
67 DoctorType(string first="", string last="", string type="");
68
69};
70
71string DoctorType::getSpeciality();

Replace the semicolon with " const". Your member function is declared
'const' and the definition is missing it.

Fix this and see if it compiles. If it still doesn't, examine your code
more attentively for other errors.

(I could do that for you, of course, but, you see, you have those line
numbers all over the place and that prevents me from copying your code
from your post and pasting it into my text editor to compile and see
what other things might be incorrect)
72{
73 return drtype;
74}
75
76void DoctorType::setSpeciality(string dtype)
77{
78 drtype = dtype;
79}
80
81
82DoctorType::DoctorType(string first="", string last="", string type="")
83 : PersonType(string first="", string last="")

The part after the ':' (not the '::') is the initialization of the base
class. You can think of it as a call to 'PersonType' constructor
function. What do 'string ' and '=""' do there? Read up on
contstructor syntax.
84{
85 setSpeciality(type);
86}






********And here are the errors:
Error 1 error LNK2001: unresolved external symbol _mainCRTStartup`
Error 2 error LNK1120: 1 unresolved externals
3 IntelliSense: explicit type is missing ('int' assumed) (Line 67)
4 IntelliSense: name followed by '::' must be a class or namespace name (Line 71)
5 IntelliSense: expected a declaration (Line 71)
6 IntelliSense: name followed by '::' must be a class or namespace name (Line 76)
7 IntelliSense: identifier "drtype" is undefined (Line 78)
8 IntelliSense: name followed by '::' must be a class or namespace name (Line 82)
9 IntelliSense: expected a '{' (Line 83)
10 IntelliSense: identifier "setSpeciality" is undefined (Line 85)

And last, and probably least, don't pass by value. Pass by const reference.

V
 
S

Steph W.

Thank you, Victor!

Sorry for the format, I thought it would be easier to read. I'll remember that next time.

I actually saw my problem. I misspelled DoctorType as DotorType. I corrected it and that fixed most of my problems. The only thing that I have a problem with now is this line:

DoctorType::DoctorType(string first="", string last="", string type="") : PersonType(string first="", string last="")
{
setSpeciality(type);
}

The compiler says "IntelliSense: "string" is not a nonstatic data member or base class of class "DoctorType". I thought what I was doing was legal, but I'm not sure why I'm getting the error.
 
W

woodbrian77

Thank you, Victor!

Sorry for the format, I thought it would be easier to read. I'll remember that next time.

I actually saw my problem. I misspelled DoctorType as DotorType. I corrected it and that fixed most of my problems. The only thing that I have a problem with now is this line:

DoctorType::DoctorType(string first="", string last="", string type="") : PersonType(string first="", string last="")

{
setSpeciality(type);
}


The compiler says "IntelliSense: "string" is not a nonstatic data member or base class of class "DoctorType". I thought what I was doing was legal, but I'm not sure why I'm getting the error.

Your initializer list looks wrong. Maybe it should be
: PersonType("", "")

Brian
Ebenezer Enterprises - So far G-d has helped us.
http://webEbenezer.net
 
R

red floyd

Thank you, Victor!

Sorry for the format, I thought it would be easier to read. I'll remember that next time.

I actually saw my problem. I misspelled DoctorType as DotorType. I corrected it and that fixed most of my problems. The only thing that I have a problem with now is this line:

DoctorType::DoctorType(string first="", string last="", string type="") : PersonType(string first="", string last="")
{
setSpeciality(type);
}

The compiler says "IntelliSense: "string" is not a nonstatic data member or base class of class "DoctorType". I thought what I was doing was legal, but I'm not sure why I'm getting the error.

Because you misspelled it in the class declaration"Dotortype"
(note the lack of a 'c').
 
T

Tobias Müller

I thought I was doing everything correctly, but when I complied the code
I had several IntelliSense errors to pop up.

Just one thing: Intellisense errors are not from the compiler but from
intellisense. Intellisense is the system that creates all those
code-completion hints and refactoring tools.

But be aware: the compiler and intellisense use different algorithms (at
least in VS 2010) and they don't always agree. Sometimes you can build
successfully even if there are still intellisense errors. I had even once a
case where after fixing all intellisense errors the code wouldn't compile
anymore.

In my experience intellisense is usually right, but that buys you nothing
because in the end it's the compiler that matters, not intellisense.

Tobi
 
B

Barry Schwarz

I'm trying to create a derived class called DotctorType from a base class called PersonType. I thought I was doing everything correctly, but when I complied the code I had several IntelliSense errors to pop up. I can't see what's causing the errors. Can anyone else see what I'm doing wrong? Here is the code:

01#include <string>
02#include <iostream>
03using namespace std;
04
05class PersonType
06{
07public:
08 // Function to output the first name and the last name.
09 void print() const;
10
11 //Function to set the first name and the last name.
12 void setName(string first, string last);
13
14 //Function to return the first name.
15 string getFirstName() const;
16
17 //Function to return the last name.
18 string getLastName() const;
19
20 // Constructor to set the first name and the last name.
21 PersonType(string first="", string last="");
22
23private:
24 string firstName; // Variable to store the first name.
25 string lastName; // Variable to store the last name.
26};
27
28// Function to output the first name and the last name.
29 void PersonType::print() const
30 {
31 cout<<firstName<< " "<<lastName<<endl;
32 }
33
34 //Function to set the first name and the last name.
35 void PersonType::setName(string first, string last)
36 {
37 firstName = first;
38 lastName = last;
39 }
40
41 //Function to return the first name.
42 string PersonType::getFirstName() const
43 {
44 return firstName;
45 }
46
47 //Function to return the last name.
48 string PersonType::getLastName() const
49 {
50 return lastName;
51 }
52
53 // Constructor to set the first name and the last name.
54 PersonType::personType(string first, string last)
55 {
56 setName(first, last);
57 }
58
59class DotorType : public PersonType
60{
61private:
62 string drtype;
63
64public:
65 string getSpeciality() const;
66 void setSpeciality(string dtype);
67 DoctorType(string first="", string last="", string type="");
68
69};
70
71string DoctorType::getSpeciality();
72{
73 return drtype;
74}
75
76void DoctorType::setSpeciality(string dtype)
77{
78 drtype = dtype;
79}
80
81
82DoctorType::DoctorType(string first="", string last="", string type="")
83 : PersonType(string first="", string last="")
84{
85 setSpeciality(type);
86}

Don't the default values belong only in the prototype? Isn't
PersonType being invoked and not defined? Shouldn't this be

DoctorType::DoctorType(string first, string last,
string type)
: PersonType(first, last)
{
setSpeciality(type);
}
 
J

James Kanze

On 7/5/2013 5:15 PM, (e-mail address removed) wrote:
Plenty. First of all, please don't put line numbers when you
post code. They just mess the code up. Post just the code,
no numbers.

I'd disagree with this. Normally, if he's encountering compiler
errors, he should also post the error. Which will refer to the
line number. And counting the lines manually is extremely error
prone (and painful). If the line numbers have a fixed format
(as his do), it's one command in the editor, once you've
copy/pasted, to eliminate them.
 
R

red floyd

I'm trying to create a derived class called DotctorType from a base class called PersonType. I thought I was doing everything correctly, but when I complied the code I had several IntelliSense errors to pop up. I can't see what's causing the errors. Can anyone else see what I'm doing wrong? Here is the code: [redacted]
58
59class DotorType : public PersonType

This should be "DoctorType", not "Dotortype". All your other error
derive from this.
 
I

Ian Collins

James said:
I'd disagree with this. Normally, if he's encountering compiler
errors, he should also post the error. Which will refer to the
line number. And counting the lines manually is extremely error
prone (and painful).

Which is why most posters tag the line(s) with a comment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top