Reputation: 11
I have three .h and three .cpp files along with them.
I have made an object of a class in the first .h (say 1.h) in a class that is in 2.h. I want to use that class object in my 3.cpp.
1.h
class One
{
bool pressed;
...
}
2.h
#include "1.h"
Class Two
{
public:
One object;
...
}
3.h
#include "2.h"
Class Three
{ ...
}
3.cpp
#include "3.h"
void Three::OnPressed()
{
object.pressed = true;
}
It allows me to make the object without complaints, however, my program gives this error when run:
error C2065 'object': undeclared identifier
I don't think this is a hard question, but I had trouble trying to explain my problem through a search bar. If you could help me out I'd appreciate it.
Upvotes: 0
Views: 72
Reputation: 596256
OnPressed()
is a member of Three
, but Three
does not derive from Two
, so Three
does not have any object
member that OnPressed()
can access. That is what the compiler is complaining about.
You would need to either:
make Three
derive from Two
class Three : public Two
give Three
a member that is an instance of One
(just like you did with Two
):
class Three
{
public:
One object;
void OnPressed();
...
};
void Three::OnPressed()
{
object.pressed = true;
}
Or give it an instance of Two
:
class Three
{
public:
Two object2;
void OnPressed();
...
};
void Three::OnPressed()
{
object2.object.pressed = true;
}
Upvotes: 1