UPDATE: Let me clarify my files and reiterate my question:
main.h
#include "other.h"
class MyClass
{
public:
void MyMethod();
void AnotherMethod();
OtherClass test;
}
main.cpp
#include "main.h"
void MyClass::MyMethod()
{
OtherClass otherTemp; // <--- instantitate OtherClass object
otherTemp.OtherMethod(); // <--- in this method here, is where I want to also call MyClass::AnotherMethod()
}
void MyClass::AnotherMethod()
{
// Some code
}
other.h
class OtherClass
{
public:
void OtherMethod();
}
other.cpp
#include "other.h"
void OtherClass::OtherMethod()
{
// !!!! This is where I want to access MyClass::AnotherMethod(). How do I do it?
// I can't just instantiate another MyClass object can I? Because if you look above in
// main.cpp, this method (OtherClass::OtherMethod()) was called from within
// MyClass::MyMethod() already.
}
So basically what I want is this: you instantiate object A, which in turn instantiates object B, which in turn calls a method that is from object A. I'm sure something like this is possible, but it might just be poor design on my part. Any direction would be greatly appreciated.
Some do one .h/.cpp per class:
my.h
#ifndef MY_H
#define MY_H
#include "other.h"
class MyClass
{
public:
void MyMethod();
OtherClass test;
}
#endif // MY_H
other.h
#ifndef OTHER_H
#define OTHER_H
class OtherClass
{
public:
void Othermethod();
}
#endif // OTHER_H
my.cpp
#include "my.h"
void MyClass::MyMethod() { }
other.cpp
#include "other.h"
#include "my.h"
void OtherClass::OtherMethod)()
{
// (ab)using MyClass...
}
If you're using only Visual Studio, you could use #pragma once instead of #ifndef xx_h #define xx_h #endif // xx_h.
EDIT: as the comment says, and also the related Wikipedia page, #pragma once is also supported (at least) by GCC.
UPDATE:
About the updated question, unrelated to #include, but more about passing objects around...
MyClass already has an embedded instance of OtherClass, test. So, in MyMethod, it's probably more like:
void MyClass::MyMethod()
{
test.OtherMethod();
}
And if OtherMethod needs to access the MyClass instance, pass this instance to OtherMethod either as a reference, or a pointer:
By reference
class OtherClass { public: void OtherMethod(MyClass &parent); }
void MyClass::MyMethod() { test.OtherMethod(*this); }
void OtherClass::OtherMethod(MyClass &parent)
{
parent.AnotherMethod();
}
By Pointer
class OtherClass { public: void OtherMethod(MyClass *parent); }
void MyClass::MyMethod() { test.OtherMethod(this); }
void OtherClass::OtherMethod(MyClass *parent)
{
if (parent == NULL) return; // or any other kind of assert
parent->AnotherMethod();
}
Define the function outside of either class in a C++ source file, not in the header:
void OtherClass :: Othermethod() {
// call whatever you like here
}
Create a .cpp file:
#include "main.h"
void OtherClass::Othermethod()
{
MyClass m; //ok :)
m.MyMethod(); //also ok.
}
Implementations don't belong in headers anyway.
Just #include it inside other.cpp
Related
The situation is like this:
//header file
#ifndef CLASSA_H
#define CLASSA_H
#include <stdint.h>
class ClassA
{
public:
void function1();
void function2();
};
#endif //CLASSA_H
//cpp file
#include "ClassA.h"
void ClassA::function1()
{
/* some code */
}
void ClassA::function2()
{
/* some more code */
}
void function3()
{
/* more code /*
}
//main.cpp
#include "ClassA.h"
int main()
{
ClassA obj;
obj.function3();
}
I want to call function3() in main.cpp but without inheritance. I tried using the instance of ClassA but that says "function3 is not a member of classA".
Maybe I'm missing a concept, would be great if anyone can help.
When you try to call function3() with obj.function3() syntax, the compiler then tries to find if there's a function exists named function3 in the instance of the class (i.e. object).
Now, in this case, you can do two things:
Include void function3() in the public: of the class and change void function3() to void ClassA :: function3() to tell the compiler that the containing code is defined for the class member function.
Or, define the function inside the header file and prevent calling the function by defining the class object. It's obvious, isnce you can't access something which is declared outside of the class.
The code explanation of all the two above methods is as follows:
Method 1
ClassA.cpp
#include "ClassA.h"
...
void ClassA::function3() // definition of the class member function
{
/* more code */
}
main.cpp
#include "ClassA.h"
int main()
{
ClassA obj;
obj.function3(); // now it's inside the class member function
// hence, we may now use it
}
ClassA.h
...
class ClassA
{
public:
...
void function3(); // declared inside the class
};
#endif //CLASSA_H
Method 2
ClassA.h
...
void function3()
{
/* more code */
}
main.cpp
#include "ClassA.h"
int main()
{
function3();
}
ClassA.cpp
class {
...
};
void function3()
{
/* more code */
}
But the second method will make the class useless, therefore, possibly you meant to achieve the first method and not the second.
I got two classes separated in four files. The main class includes a sub class and needs to execute functions of it (not shown in the minimal example code). What I want to do is to execute a function of the main class in the scope of the subclass.
I think some ideas would be to inherit the functions in the sub class but I could not figure out how to do this.
MainClass.cpp
#include "MainClass.hpp"
void MainClass::mainCallback() {
std::cout << "[mainCallback] executed" << std::endl;
}
void MainClass::subCallback() {
std::cout << "[subCallback] executed" << std::endl;
}
int main() {
MainClass mainClass;
mainClass.mainCallback();
SubClass subClass;
subClass.activateSubClass();
return 0;
}
MainClass.hpp
#pragma once
#include "SubClass.hpp"
#include <iostream>
class MainClass{
public:
void mainCallback();
void subCallback();
};
SubClass.cpp
#include "SubClass.hpp"
void SubClass::activateSubClass(){
mainClass.subCallback(); //TODO call this function from this scope
}
SubClass.hpp
#pragma once
class SubClass{
public:
void activateSubClass();
};
The error in SubClass.cpp is of course:
error: use of undeclared identifier 'mainClass'
Just subclass the subclass:
class SubClass: public MainClass {
public:
void activateSubClass();
};
This (public) way the SubClass makes all methods of MainClass callable in SubClass instances. You could also private inherit. That way only activateSubClass() 'ld be callable.
In activateSubClass you can call directly the methods of the parent class:
void SubClass::activateSubClass(){
mainClass.subCallback(); //TODO call this function from this scope
}
Don't forget to include MainClass.hpp in SubClass.hpp
You try to call a MainClass.subCallback() without having an instance of MainClass. According to me, this is the typical use case for static methods.
Then, I think you make your #include directives the wrong way. Indeed, MainClass does not seem to need to know SubClass but the opposite is true. I think it is better to include MainClass.hpp in SubClass.hpp. This will solve your circle dependencies problem.And you can write your main() function in another file.
EDIT: Example
MainClass.hpp:
#pragma once
class MainClass
{
public:
void mainCallback();
static void subCallback(); // mak it static to be able to call it without an instance of the class
};
MainClass.cpp:
#include "MainClass.hpp"
#include <iostream>
void MainClass::mainCallback()
{
std::cout << "[mainCallback] executed" << std::endl;
}
void MainClass::subCallback()
{
std::cout << "[subCallback] executed" << std::endl;
}
SubClass.hpp:
#pragma once
class SubClass
{
public:
void activateSubClass();
};
SubClass.cpp:
#include "SubClass.hpp"
#include "MainClass.hpp" // MainClass inclusion
void Suclass::activateSubClass()
{
MainClass::subCallback(); // Call of the static method
}
main.cpp:
#include "MainClass.hpp"
#include "SubClass.hpp"
int main()
{
MainClass mc;
mc.mainCallback();
SubClass sc;
sc.activateSubClass(); // Will call MainClass::subCallback()
return 0;
}
I hope it can help you.
I am trying to implement the Observer pattern for a game I am creating for a school project.
I have created 2 virtual classes, Observer and Observable.
Observer.h:
#ifndef OBSERVER_H
#define OBSERVER_H
#include <vector>
class Observable;
class Observer
{
public:
Observer();
virtual ~Observer();
virtual void update(Observable* ob) =0;
};
#endif
Observer.cpp:
#include "stdafx.h"
#include "Observer.h"
Observer::Observer()
{
}
Observer::~Observer()
{
}
Observable.h:
#ifndef OBSERVEABLE_H
#define OBSERVEABLE_H
#include <vector>
#include "Observer.h"
class Observable
{
protected:
std::vector<Observer*> observers;
public:
Observable();
virtual ~Observable();
virtual void attach(Observer *a);
virtual void detach(Observer *a);
virtual void notify();
};
#endif
Observable.cpp:
#include "stdafx.h"
#include "Observable.h"
Observable::Observable()
{
}
Observable::~Observable()
{
}
void Observable::attach(Observer *a)
{
observers.push_back(a);
}
void Observable::detach(Observer *a)
{
for (auto it = this->observers.begin(); it < this->observers.end(); it++)
{
if (*it == a)
{
this->observers.erase(it);
break;
}
}
}
void Observable::notify()
{
for (int i = 0; i < observers.size(); i++)
observers[i]->update(this);
}
I have a Map class that inherits from Observable, and a mapView class that inherits from Observer (Map is very long, I only included the relevant functions)
Map.h:
#ifndef MAP_H
#define MAP_H
#include "Observable.h"
#include <iostream>
class Map : public Observable
{
public:
Map();
~Map();
void getLatest();
void notify();
};
#endif
Map.cpp:
#include "stdafx.h"
#include "Map.h"
Map::Map()
{
}
Map::~Map()
{
}
void Map::getLatest()
{
using namespace std;
cout << "This is the latest info!" << endl;
}
mapView.h:
#ifndef MAP_V_H
#define MAP_V_H
#include "Observer.h"
#include "Map.h"
#include "Plants.h"
class mapView : public Observer
{
public:
mapView();
~mapView();
void update(Map* map);
};
#endif
mapView.cpp:
#include "stdafx.h"
#include "mapView.h"
#include "Map.h"
mapView::mapView()
{
}
mapView::~mapView()
{
}
void mapView::update(Map* map)
{
map->getLatest();
}
Finally, my main simply creates a Map and a mapView, attaches the mapView, and calls map.notify()
main.cpp:
#include "stdafx.h"
#include "setUp.h"
#include "Map.h"
#include "mapView.h"
int main()
{
Map gameMap;
mapView view;
gameMap.attach(&view);
gameMap.notify();
return 0;
}
I run into a number of issues here. I cannot create a mapView item because the compiler says I never implemented an override version of update(Observable* ob).... I tried with update(Map* map) but it appears that despite the fact that Map inherits from Observable, it does not seem to count as the same signature, so it won't compile.
I attempted to change my mapView::update() function to take a pointer to Observable instead, but this won't work because the function calls something from Map class.
I then tried changing the update function to NOT be a virtual function (with empty implementation in the virtual class), but it seems any time I try to pass a Map to update, it will call the base class function and not the mapView version. In other words, getLatest() is never called.
I am now pretty confused because this sort of goes against how I thought polymorphism worked. Would appreciate some help or insight if possible!
Thank you,
Your base class declares:
virtual void update(Observable* ob) =0;
You derived class declares:
void update(Map* map);
These are not the same signature. If you used the new override keyword, you would see at compile time that you were not in fact overriding the virtual method.
If you know you'll only get Maps, then you can just use static_cast. But it's safer to use dynamic_cast:
void update(Observable* o) override { // now we're ok
if (auto map = dynamic_cast<Map*>(o)) {
// okay, got a Map
// ....
}
else {
// huh?
}
}
Super brief type theory digression. The typical rule for overrides is covariant in return and contravariant in the argument type. You can specify a more-derived return type, or a more-base argument type. Think about it this way - if you have a base class function taking and returning a Car*... your argument can be a Car* (that's exactly what's expected), or it can be a Vehicle* (since anything you can do with a Vehicle, you can do with a Car - this still works), but it can't be a SportsCar* (since the caller might pass you a Car that isn't a SportsCar and justifiably expect this to work!) It doesn't make sense for the derived class to accept only Maps - you have to be able to accept any Observables, even not Maps!
Trying to pass a parent class object to a child class object so that the child class object has control over the parent class object's methods.
This is however resulting in header related issues.
I've tried forward declaring one of the classes but it seems whatever class is declared first always has trouble reading from the class declared below.
Both errors refer to Device' constructor where try to call dm's hello world method, they are:
Use of undefined type 'DeviceManager'
Left of '->HelloWorld' must point to class/struct/union/generic type
...
//main.cpp
#include "parent.h"
void main()
{
cout << "Created DeviceManager\n";
DeviceManager* deviceManager = 0;
deviceManager = new DeviceManager;
cout << "Giving DeviceManager a device\n";
deviceManager->p = new Device(deviceManager);
cout << "Giving Device a reference to DevicenManager\n";
deviceManager->Share();
}
...
class DeviceManager;
class Device
{
public:
Device(DeviceManager* manager)
{
dm = 0;
this->dm = manager;
this->dm->HelloWorld();
}
DeviceManager* dm;
};
//device manager
class DeviceManager
{
public:
DeviceManager()
{
p = 0;
}
void HelloWorld()
{
//if this calls we know the child has control over the parent.
cout << "Hello World";
}
Device* p;
};
Yes.
To solve circular dependencies with class member and function declarations, you can forward-declare a class:
class A;
class B {
A *a;
};
class A {
B *b;
};
To define class member functions that access members of the other class, you must define the function after the other class has been defined:
class B;
class A {
public:
void f(B &arg);
};
class B {
public:
void g(A &arg);
};
void A::f(B &arg) {
arg.g(*this);
}
void B::g(A &arg) {
arg.f(*this);
}
Usually, in a C++ project, you wouldn't even encounter this problem: You would put function definitions, i.e. implementations, into .cpp files, while putting the class definitions into header files. Class forward declarations, if neccesary, could be put into their own header files that are included by all headers that need them.
A full example of how you would split the above code into multiple files:
a.cpp
#include "a.h"
#include "b.h"
void A::f(B &arg) {
arg.g(*this);
}
b.cpp
#include "b.h"
#include "a.h"
void B::g(A &arg) {
arg.f(*this);
}
a.h
#ifndef _A_H_
#define _A_H_
#include "forward_declarations.h"
class A {
public:
void f(B &arg);
};
#endif //_A_H_
b.h
#ifndef _B_H_
#define _B_H_
#include "forward_declarations.h"
class B {
public:
void g(A &arg);
};
#endif //_B_H_
forward_declarations.h
#ifndef _FORWARD_DECLARATIONS_H_
#define _FORWARD_DECLARATIONS_H_
class A;
class B;
#endif //_FORWARD_DECLARATIONS_H_
As a general rule of thumb, if you need to forward-declare a class, you might have misdesigned something and should think about whether there is a better way (but there also are perfectly valid use cases that require class forward declarations).
If you don't understand my #ifndef, #define and #endif preprocessor lines: These are header guards, and should be used with all files that are included somewhere else, exception you know precisely what you're doing. Believe me. You'll regret ommiting one.
If your problem is cyclic dependancy, like this:
// DeviceManager.h
#include "device.h"
class DeviceManager
{
DeviceManager(Device& device) {}
};
// Device.h
#include "DeviceManager.h"
class Device
{
Device(DeviceManager& manager) {}
};
You can solve the problem be forward declaring one of the classes, and passing the object by pointer.
// Device.h
//#include "DeviceManager.h"
class DeviceManager;
class Device
{
Device(DeviceManager* manager) {}
};
i have been attempting to pass an object into a function that belongs to a class both classes are in there own files...but when i try to pass the object as an argument for the function prototype it gives me an error saying that the object doesn't exist... ill provide some pseudo code to demonstrate my problem
//class 1 .h
class Class1
{
public:
void function(Class2);//this is were one of my errors
};
//class 1 .cpp
void Class1::function(Class2 object )//another error
{
//stuff happens
}
//main.cpp
//then i simply call these functions like this
Class1 object;
Class2 object2;
int main()
{
object.function1(object2);
return 0;
}
and i get errors "Class2' has not been declared"
and errors about Class1 prototype does not match any classes....
if someone could explain what im doing wrong it would be a great help also if more code is needed just ask and i will post it.
EDIT
when i was attempting to include class2`s header in class one i was using the wrong director as i forgot i had separated .h files into there own folder anyway now i have fixed that it all work thanks a lot everybody.
You'll need to include Class2's header file in Class1.h. That is:
//////////////////
//Class1.h
#include "Class2.h"
class Class1
{
public:
void function(Class2 arg);
};
If you are only using a pointer to Class2 as an argument, then you can forward declare Class2 instead of including the header, that is:
//////////////////
//Class1.h
//Forward declare Class2 so the compiler knows the name exists
class Class2;
class Class1
{
public:
void function(Class2 *arg);
};
There's some more info here if you're interested.
Well, in fact Class2 has not been declared!
So,
// Class2.h:
class Class2
{
};
// Class1.h:
#include "Class2.h"
class Class1
{
public:
void function(Class2 object);
};
// Class1.cpp:
#include "Class1.h"
void Class1::function(Class2 object)
{
//stuff happens
}
// main.cpp:
#include "Class1.h"
Class1 object;
Class2 object2;
int main()
{
object.function1(object2);
return 0;
}
Have you tried doing this yet?
main.cpp:
#include "class1.h"
...
int main()
{
Class1 object;
Class2 object2;
object.function1(object2);
return 0;
}
The trick is to use the #include "class1.h" at the top of your main.cpp file
Actually you need to make the compiler aware of the classes that u are going to use in the file anywhere.
Have a look at a very similar Question : http://www.gamedev.net/topic/553424-c-calling-member-of-another-class-in-another-file/