-
January 18th, 2016, 04:30 PM
#1
Classes
Hi guys,
The following code is a small part of a program I want to write for a question. The question says this: Create a class named B with a data member (e.g., a string) and another class named D with its own data member (say, another string) and that class D holds a value of class B.
I wrote the code below for it but face an error saying: identifier "_n" is undefined. But I have defined it in D(int _i, string _n).
What is the reason please? An how to solve it?
Code:
#include <iostream>
using namespace std;
class B {
public:
B(string n) : name(n) {}
string name;
};
//--------------------------
class D : public B {
public:
D(int _i, string _n) : B(_n),i(_i) {}
int i;
B b1(_n);
};
//--------------------
int main() {
return 0;
}
-
January 18th, 2016, 04:36 PM
#2
Re: Classes
Why do you think that _n is defined here when it is only a parameter for the constructor?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 18th, 2016, 04:59 PM
#3
Re: Classes
OK. I changed class D with this:
Code:
class D : public B {
public:
D(int _i, string _n) : B(_n), i(_i), nd(_n) {}
int i;
string nd;
B b1(nd);
};
But now get these:
Error 1 error C2061: syntax error : identifier 'nd' 18
2 IntelliSense: member "D::nd" is not a type name 18
-
January 18th, 2016, 05:10 PM
#4
Re: Classes
Correct! The error message should give a clue. If you saw the statement
what would you think it was?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 18th, 2016, 05:22 PM
#5
Re: Classes
I don't know what you mean really but B b1(int); is not correct because the class B gets an variable for its objects not a type. As far as it seems to me B b1(nd); is correct because nd is a variable string, what the class B needs to its objects.
-
January 18th, 2016, 05:29 PM
#6
Re: Classes
look like a function declaration! So to the compiler
also looks like a function declaration with an unknown type for the parameter - hence the error message regarding unknown type !
Try
Similarly, your original class D could be
Code:
class D : public B {
public:
D(int _i, string _n) : B(_n), i(_i) {}
int i;
B b1 = B(name);
};
PS I don't know what compiler you are using, but with universal initialisation you could do
or
Last edited by 2kaud; January 18th, 2016 at 05:47 PM.
Reason: PS
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 19th, 2016, 06:44 AM
#7
Re: Classes
FWIW, you assignment doesn't say anything about deriving D from B.
-
January 19th, 2016, 07:45 AM
#8
Re: Classes
Thank you 2Kuad.
B b1(nd); I don't know why it should look like a function declaration! nd is already defined.
B b1 = B(nd); seems odd to me. I have studied about class derivations but I don't recall that kind of making an object. Likely I've forgotten it. I think I should review that section of my book.
@GCDEF: Thanks for your advice. Would you please explain more?
And another question please:
If you had the question saying the following, what simplest code would you write for it? I used derivation method because I thought it's the only possible way (for having a value of a class as a member of another class) and it's also simple.
Question: Create a class named B with a data member (e.g., a string) and another class named D with its own data member (say, another string) and that class D holds a value of class B.
-
January 19th, 2016, 08:08 AM
#9
Re: Classes
Originally Posted by tomy12
Thank you 2Kuad.
B b1(nd); I don't know why it should look like a function declaration! nd is already defined.
B b1 = B(nd); seems odd to me. I have studied about class derivations but I don't recall that kind of making an object. Likely I've forgotten it. I think I should review that section of my book.
@GCDEF: Thanks for your advice. Would you please explain more?
And another question please:
If you had the question saying the following, what simplest code would you write for it? I used derivation method because I thought it's the only possible way (for having a value of a class as a member of another class) and it's also simple.
Question: Create a class named B with a data member (e.g., a string) and another class named D with its own data member (say, another string) and that class D holds a value of class B.
There's not much more to say. You've derived D from B as in
class D : public B
which means D is derived from B. Your assignment doesn't say to do that. All you need is
class D
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|