Derived Classes

Hi guys,

The following code is a small part of a program I want to write. I face an error saying: identifier "_n" is undefined. But I have defined it in D(int _i, string _n). What is the reason please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;
}



Last edited on
Uhm, you don't have a string variable called _n anywhere?

D(int _i, string _n) : B(_n),i(_i) {}

You have a variable called int i; so that's all good. But you're using _n which doesnt exist.
Last edited on
You have a variable called int i; so that's all good. But you're using _n which doesnt exist.
Yeah, you are right.

So how to have a value of class B as a member of class A please?
PS: By value I think the author has meant an object.
Last edited on
I think your problem is line 17. You should probably just remove that line.
So how to have a value of class B as a member of class A please?

Just declare it the same as you would any other type of member, e.g.

1
2
3
4
5
6
7
8
class MyClass
{
  int myInt;
  float myFloat;
  std::string myString;

  // etc for any other type
}


I'm slightly concerned that D inherits from B, and also contains an object of type B among its members. Are you sure you want to do both those things?
Last edited on
@Peter:
I think your problem is line 17. You should probably just remove that line.

But I need that value (or object). Because the question wants it :(

@MikeyBoy:
I'm slightly concerned that D inherits from B, and also contains an object of type B among its members. Are you sure you want to do both those things?

No the question does not want it. It just says: "class D (for example) should hold a value of class A". And I thought since class A has a string data member so we should initialize it when creating an object of it also with the members of class D.

I wrote this one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

class B {
public:
	B(string n) : name(n) {}
	string name;
};

//--------------------------

class D {
public:
	D(int _i, string _n) : i(_i),_name(_n) {}
	
	int i;
	string _name;
	B b1(_name);
	
};

//--------------------

int main() {
	return 0;
}


I get this error:
1 IntelliSense: member "D::_name" is not a type name line 18
Last edited on
Go back and look again at me previous answer. I show you how to declare a data variable there. In fact, you're declaring other data members just fine already in your class, so I don't understand why you've decided to do something completely different for b1.

Also, why do you have different naming conventions for your data members? You use an underscore prefix in _name, but not in i or b1.
I DON'T KNOW HOW TO SPEAK TO BE UNDERSTOOD. :(:(!!!!!!!!
Do you want to help me?
Write a simple class (e.g., D) that holds a value of another class (e.g., B). That's all.
Write a simple class (e.g., D) that holds a value of another class (e.g., B).



1
2
3
4
5
6
7
8
9
10
11
12
13
14
class B
{
  int x;
};

class D
{
  B an_object_of_type_B;
};

int main()
{
  D an_object_of_type_D;
}

Last edited on
B has not a default constructor so B an_object_of_type_B; makes an error probably.
Never mind guys. I try to get what I want from elsewhere. Thanks.
I would think it was one the most simple questions one person can have.
Last edited on
Did you try building it? It compiles.

When you create a class, if you do not specify any constructors, you get the default constructor for free.

I would think it was one the most simple questions one person can have.

I can't believe you think that this extremely simple code doesn't compile. This is literally the simplest answer possible but you don't know enough C++ to understand, AND you didn't actually try it. All you had to do was push that little button next to the code and see it compile. Pathetic.

Once you've done that, don't come back.

Your pathetic, passive-aggressive whining is something that would be shameful in a five year old child. You're not mature enough to be on a grown-up website with adults.

Last edited on
Are you OK?
Why are you that anger? What made you talk an a way with full of insultation? I just said never mind and thank you.
Try to learn some manners foolish man. And try no to be that rude.
You don't deserve manners. You are pretending to be civilised and polite while actually insulting people. These are the actions of a passive-aggressive child. Any time you are ready to start acting like an adult, you'll be treated like one.
Last edited on
OK. Learn me some manners. Consider it as the first time for me. Where did I insult people? Please answer that question.
And now sea-lioning. Is this your first time on the internet? End of conversation.
Why didn't you answer that question? I know, because you have no answer. Reread my post and see I said "never mind and thank you and trying to get help somewhere else". Are these insultaions?!
I'm really sorry for you. You even don't deserve to be insulted.
@Kubani

Stuff like:

Do you want to help me?

and:

I would think it was one the most simple questions one person can have.

sound sneering and aggressive, like you're the one asking simple, easy questions, and we're somehow being stupid for not understanding you.

Moschops' code compiled and worked fine; your comment "makes an error probably" suggests that you didn't even bother compiling it, or trying to understand it, instead just assuming that a much more knowledgeable person must have given you incorrect code - when, in fact, the code they posted was fine.

You failed to make any effort to try and understand the difference between their code and yours, and why their code might work when yours doesn't, and instead went straight to assuming their code was "probably" wrong.

There's a simple reason why your code doesn't compile when Moschops' code does, and if you'd just told us of the errors you were finding when following our suggestions, and asking what the solution was, I'm sure Moschops or I, or someone else, would have happily told you.

Instead, you just insultingly assumed that you knew better, and implied that we were somehow at fault for failing to deal properly with your "simple" question.
Last edited on
Topic archived. No new replies allowed.