I have a class I'll call class_A that creates an instance another class let's say class_B when it's constructor is called. The issue I'm having is that when I create and instance of class_A I'm having trouble accessing functions of the class_B that should be associated with it.
Each class has it's own class and header file and I've put in the #include for each. I'm also using a global class pointer.
class_A.cpp
class_B* B;
class_A::class_A(void)
{
B = new class_B(this);
}
class_B.cpp
class_A* A;
class_B::class_B(class_A* this_A)
{
A = this_A;
}
void class_B::class_B_method()
{
//code here
}
main.cpp
void _tmain(int argc, _TCHAR* argv[])
{
//create a new instance of class_A which also creates a new instance of class_B
class_A* new_A = new class_A();
//the following code is not accurate
new_A->B->class_B_method();
}
Going to take a shot. Not sure all of what you are doing but it looks like class_A.cpp has a global B pointer that gets set in the A constructor. In class_B.cpp you have a global A pointer that gets set in the B constructor. But in the main.cpp new_A tries to reference B but it looks as if B is not a member of A but a global so new_A->B wont work. B would have to be a member of the A class for that to work.
Do you have a member B in the class_A?
Alright that makes sense, and I can now see it in my main but I'm still unable to access the members of the class_B object. And other methods in class_A cannot utilize the object. My IDE reports them as "expression must have pointer-to-class type" That's the reason I used a global variable to begin with.
I appreciate the help you've given me so far. Any more insight would be great, I'll continue to research off of what you've said so far. Somehow I've got to make the class object in my header reachable by other methods in my class and reference the methods of the object once it's been initialized.
Why do you have to access B from inside of A? With the global you can just access B functions by just using the B pointer. Not that I recommend using a global.
The idea is that class B is an object found inside of class A, so each time I create an instance of class A, there will be a unique instance of class B created. Am I approaching this wrong for c++? This concept makes sense to me in java or c#.
Is the only person that wants to talk to class_B, class_A?
If yes, then you can define class_B all in side of class_A and make class_B a member. If can be created in the constructor or a new in the constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class class_A {
public:
// Define B
class class_B {
public:
class_B(){};
};
class_A() {};
private:
class_B B;
// or
class_B *ptrB;
};
If no, then you have to define class_B and class_A and include class_B.h in the class_A.h file.
In either every case class_A would have a unique instance of class_B.
It is object composition where you combine simple objects or data types into more complex ones. http://www.learncpp.com/cpp-tutorial/102-composition/
Like a computer is made up of a CPU, disks, keyboard, screen and etc... The class computer is made up of all of the other classes CPU, disks, etc...
Also if you use the new keyword to create the object, then your reference to the object needs to be a pointer, not a name.
1 2 3 4 5
MyObject o = new MyObject() ; // will fail; probably won't even compile...?
MyObject *p = new MyObject() ; // will work; returns pointer to a MyObject created with default constructor
MyObject n ; // will also work; calls default MyObject constructor
p->method() ; // calls member function MyObject::method() for instance at *p
n.method() ; // calls member function MyObject::method() for instance named n
MyObject n(); That's a function declaration. It returns a MyObject and receives nothing. MyObject n; An object constructed with the default constructor.
Thanks for the link to composition. I decided to recreate that using .h an .cpp files to work on my c++ skills. It compiled successfully but now after modeling my project after it I am still running into a an error while compiling. There are more parts to the project but I'm focusing on the top level for now until I have a more firm grasp on my errors.
Below is my source. The error occurs in Room.h on line 11 where I declare "Thermostat thermostat_in_room;"
room.h(11): error C2146: syntax error : missing ';' before identifier 'thermostat_in_room'
room.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
room.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Thanks for the link ne555, telling me what I needed to do is one thing, showing me why is much better!
Removing the Room.h include does allow everything to compile. Which brings me to my next question, without including Room.h in Thermostat.h will I be able to create a pointer back to the Room object from Thermostat? I need to do this for some recursive calls. I'm going to see what I can figure out, but at this point the composition and includes make sense.
I know I'm close, and I do wish I could figure this out by just reading the information I've found but so far no such luck. Maybe I'll get it before someone points it out to me, but I doubt it ;-)
The error occurs on line 35 of thermostat.cpp when I attempt to use my pointer to the room to call a function of the room and get "use of undefined type"
#pragma once
#include <iostream>
#include <string>
class Room;
class Thermostat;
class AirConditioner
{
private:
Room* Room_location;
bool state;
std::string name;
public:
//constructor
AirConditioner(Room*);
//desctructor
~AirConditioner(void);
// return air conditioner name
std::string Get_name(void);
// turn air conditioner on and lower room temp 1 degree
void Turn_on(void);
// turn air conditioner off
void Turn_off(void);
};