I have a problem with C++.
I want to create two classes, A and B.
Class A has some methods that take an argument that is an instance of class B. But in Class B I also have some methods which take an argument that is an instance of class A.
I tried to forward declare class A and then define class B. Finally, I define class A.
Some code:
class A;
class B
{
void Method1(A* instaceOfA)
{
instaceOfA->MethodX();
}
.......
};
class A
{
Method1(B* instaceOfB);
MethodX();
.......
};
I code in Visual Studio 2010, and it shows an error because I invoke MethodX in class A but class A is not defined completely.
How can I solve this problem?
Put the definition of B::Method1 after declaration of class A
//header file
class A;
class B
{
void Method1(A* instaceOfA);
.......
};
class A
{
Method1(B* instaceOfB);
MethodX();
.......
};
// cpp file
void B::Method1(A* instaceOfA);
{
instaceOfA->MethodX();
}
This is the purpose of .cpp file. You declare the classes and methods in a header file then, add the definitions to the .cpp file.
I suggest moving implementations of methods outside class declarations, like this:
class A;
class B {
public:
void Method1(A* instanceOfA);
...
};
class A {
public:
void Method1(B* instanceOfB);
void MethodX();
...
};
void B::Method1(A* instanceOfA) {
...
}
Define and declare the classes the other way around?
class B;
class A
{
Method1(B* instaceOfB);
MethodX();
};
class B
{
void Method1(A* instaceOfA)
{
instaceOfA->MethodX();
}
};
Of course, this does not work if the A::Method1 function also is inline.
Is your impl code in the header file? If so, try moving it to a cpp file and #include A and B
Is there a reason you need to implement class B inline? If you seperate the class definitions from the implentations (ideally putting them in .h and .cpp files), this problem will go away..
//blah.h
class A;
class B
{
void Method1(A* instaceOfA);
.......
};
class A
{
Method1(B* instaceOfB);
MethodX();
.......
}l
//blah.cpp
#include "blah.h"
class B
{
void Method1(A* instaceOfA)
{
instaceOfA->MethodX();
}
.......
};
class A
{
Method1(B* instaceOfB)#
{
instaceOfB->Method1();
}
MethodX();
.......
}
Related
I am going through some code which is a little un-organized.
I would like to use a class created in a cpp file used as an argument type in its header file. I know the class needs to be defined in the header but I am currently looking for a short approach here for now. This is what I have
filename: foo.cpp
class bar {
}
Now the header
filename: foo.h
class teacher {
public:
void dosomething(bar b) { ///<-----Incomplete type.
b.work(); ///
}
}
I tried declaring a prototype inside the header this way
class foo;
class teacher {
public:
void dosomething(bar b) {
b.work(); ///Error no method work for incomplete type.
}
}
What would be the simplest way to fix this issue apart from moving the bar class to the header file
You can forward-declare the type bar in the header assuming you do nothing that relies on it being a complete type. For example, this won't work:
// foo.hpp
class bar;
class teacher {
public:
void dosomething(bar b) {
// Will fail, bar is not complete:
b.work();
}
};
But this will:
// foo.hpp
class bar;
class teacher {
public:
void dosomething(bar b);
};
// foo.cpp
class bar {
public:
void work() {}
};
void teacher::dosomething(bar b) {
b.work();
}
However, I would advise instead to create a bar.hpp file with the definition of the bar type and #include it into foo.hpp.
Consider, for example, that any code (other than foo.cpp) that includes foo.hpp will be unable to invoke teacher::dosomething(bar), since it won't have any definition for bar.
I think it will be better like this :
File : bar.hh
class bar
{
...
}
File : foo.hh
#include "bar.hh"
class teacher
{
public:
void dosomething(bar b)
{
b.work();
}
}
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 am porting code from Java to c++ and I'd like to replicate some anonymous functionalities.
In file A.h I have :
class A
{
private:
int a;
class AnonClass;
friend class AnonClass;
};
In file A.cpp I have :
namespace
{
class AnonClass
{
public:
AnonClass(A* parent)
{
parent->a = 0; // This doesn't work, a is not accessible
}
}
}
Is it possible to friend a class in an anonymous namespace in C++?
In Java you can declare anonymous classes so it would be very similar. Also it would not expose AnonClass to clients of A.h
Less known alternative is to make class Anon a member class of A. Inside class A you only need a line class Anon; -- no real code, no friend declaration. Note it goes within class A, almost as in Java. In the .cpp file you write all the details about Anon but you put it not in anonymous namespace but withinA::
class A::Anon { ..... };
You can split declaration and implementation of A::Anon, as usual, just remeber always add A:: to Anon.
The class Anon is a member of A and as such gets access to all other members of A. Yet it remains unknown to clients of A and does not clutter global namespace.
As far as I can see you can not. The reasons:
The “anonymous” namespace is accessible only within the file you created it in.
You have to define whole AnonClass class and it's functions in one namespace, i.e. in one place in the program.
Class A has to be defined before AnonClass constructor.
AnonClass has to be at least declared before class A.
So you see, you can't break AnonClass definition on two parts. And you can't define it both before and after A class.
The only option - put class A into the same anonymous namespace. This code works:
namespace
{
class A
{
public:
A():a(0){};
private:
int a;
friend class AnonClass;
};
class AnonClass
{
public:
AnonClass(A* parent);
};
AnonClass::AnonClass(A* parent)
{
parent->a = 0;
};
}
int main() {
A a;
return 0;
}
I hope this helps.
I currently have the following two classes
class TOrder
{
public:
private:
.......
};
Now my other class is :
#include "TOrder.h"
namespace namespaceA
{
namespace namespaceB
{
class OrderDis
{
private:
TOrder* frmPointer;
.....
};
}
}
The above works fine the problem starts when I use an object of OrderDis in TOrder as such
#include <QMainWindow>
#include "OrderDis" //Added - Creates Problem
class TimedOrder
{
public:
.......
};
Any suggestion on how I could use forward declaration to resolve my issue ?
You could forward OrderDispatcher in TimeOrder.h
namespaceA
{
namespaceB
{
class OrderDispatcher;
}
}
class TimedOrder
{
//...
};
The forward declaration can be written as:
namespace A{ namespace B{ class OrderDispatcher; } }
As you only use a pointer to TimedOrder in the OrderDispatcher class, it can be solved by simply not including TimedOrder.h in the OrderDispatch.h file. Instead just declare the TimedOrder class:
class TimedOrder;
No need to muck about with namespaces and such then.
Note: You can't declare it in any of the namespaces, declare it instead where you now do your #include.
Very new to c++ having trouble calling a function from another class.
Class B inherits from Class A, and I want class A to be able to call a function created in class B.
using namespace std;
class B;
class A
{
public:
void CallFunction ()
{
B b;
b.bFunction();
}
};
class B: public A
{
public:
virtual void bFunction()
{
//stuff done here
}
};
It all looks fine on screen (no obvious errors) but when I try to compile it i get an error C2079 'b' uses undefined class B.
I've tried making them pointers/ friends but I'm getting the same error.
void CallFunction ()
{ // <----- At this point the compiler knows
// nothing about the members of B.
B b;
b.bFunction();
}
This happens for the same reason that functions in C cannot call each other without at least one of them being declared as a function prototype.
To fix this issue we need to make sure both classes are declared before they are used. We separate the declaration from the definition. This MSDN article explains in more detail about the declarations and definitions.
class A
{
public:
void CallFunction ();
};
class B: public A
{
public:
virtual void bFunction()
{ ... }
};
void A::CallFunction ()
{
B b;
b.bFunction();
}
What you should do, is put CallFunction into *.cpp file, where you include B.h.
After edit, files will look like:
B.h:
#pragma once //or other specific to compiler...
using namespace std;
class A
{
public:
void CallFunction ();
};
class B: public A
{
public:
virtual void bFunction()
{
//stuff done here
}
};
B.cpp
#include "B.h"
void A::CallFunction(){
//use B object here...
}
Referencing to your explanation, that you have tried to change B b; into pointer- it would be okay, if you wouldn't use it in that same place. You can use pointer of undefined class(but declared), because ALL pointers have fixed byte size(4), so compiler doesn't have problems with that. But it knows nothing about the object they are pointing to(simply: knows the size/boundary, not the content).
So as long as you are using the knowledge, that all pointers are same size, you can use them anywhere. But if you want to use the object, they are pointing to, the class of this object must be already defined and known by compiler.
And last clarification: objects may differ in size, unlike pointers. Pointer is a number/index, which indicates the place in RAM, where something is stored(for example index: 0xf6a7b1).
class B is only declared but not defined at the beginning, which is what the compiler complains about. The root cause is that in class A's Call Function, you are referencing instance b of type B, which is incomplete and undefined. You can modify source like this without introducing new file(just for sake of simplicity, not recommended in practice):
using namespace std;
class A
{
public:
void CallFunction ();
};
class B: public A
{
public:
virtual void bFunction()
{
//stuff done here
}
};
// postpone definition of CallFunction here
void A::CallFunction ()
{
B b;
b.bFunction();
}
in A you have used a definition of B which is not given until then , that's why the compiler is giving error .
Forward declare class B and swap order of A and B definitions: 1st B and 2nd A. You can not call methods of forward declared B class.
Here's my solution to the issue. Tried to keep it straight and simple.
#include <iostream>
using namespace std;
class Game{
public:
void init(){
cout << "Hi" << endl;
}
}g;
class b : Game{ //class b uses/imports class Game
public:
void h(){
init(); //Use function from class Game
}
}A;
int main()
{
A.h();
return 0;
}
You can also have a look at the curiously recurring template pattern and solve your problem similar to this:
template<typename B_TYPE>
struct A
{
int callFctn()
{
B_TYPE b;
return b.bFctn();
}
};
struct B : A<B>
{
int bFctn()
{
return 5;
}
};
int main()
{
A<B> a;
return a.callFctn();
}