I need some help to construct or declare this kind of recursivity:
I have 2 classes A and B.
Both, A and B, have a set of similar methods that are coded in class C.
And some C methods calls A/B methods, example:
Class A {
m1;
m2 that calls m3 in class C;
}
Class B {
m1;
m2 that calls m3 in class C;
}
Class C {
m3;
m4 that calls m1 in class A or B;
}
Some times I will create an object from class A and after that I will call A.m4 and other times I will create an object from class B and will call B.m4
Each class is in a different file (I will appreciate some help on how to setup the headers in this case).
I'm kind of C++ beginner, that includes class-based programming beginner.
Still need some help to get it. For example, no pointers, references, ...
//------------------------------- main program
#include "B.h"
int main()
{
B objB;
objB.m_B();
return 0;
}
//------------------------------ Class B header
#ifndef B_H_
#define B_H_
class B
{
public:
void m_B();
};
#endif
//---------------------------- Class B implementation
#include "B.h"
void B::m_B()
{
m_A();
}
//--------------------------- Class A header
#ifndef A_H_
#define A_H_
class A
{
public:
void m_A();
};
#endif
//------------------------------- Class A implementation
#include <iostream>
#include "A.h"
void A::m_A()
{
std::cout << "Hi";
}
g++ -O3 -Wall -c -fmessage-length=0 -oB.o ..\B.cpp
..\B.cpp: In member function `void B::m_B()':
..\B.cpp:11: error: `m_A' was not declared in this scope
Build error occurred, build is stopped
You seem to have a fundamental misunderstanding of how classes work.
You can't call a member function in class A without having an instance of class A. Member functions are not like global functions -- they require an instance of the class (IE: and object) on which to perform their function. You cannot call a [non-static] member function without provided that object.
class A
{
public:
void m_A() { std::cout << "m_A called\n"; }
void m_A2()
{
m_A(); // works because it's a fellow member function.
// it perform the operation on object 'this' (ie: the object
// currently being operated on in this function)
}
};
int main()
{
m_A(); // doesn't work (what object are you calling m_A on?)
A myA; // first, make an object of the class
myA.m_A(); // then call it -- this works
return 0;
}
common() is really a common code and should be coded in a single place.
Because I was not able to do that, I had to code it twice, once inside class B and another inside class C.
I was thinking that a 3rd class ( class A ) could be the solution. common() should be there.
Notice, that start() must call common() and common must call finish().
start() and finish() are both different for B and C.
How can I do that ?
Thank you
Marcio
//------------------------------------------- main program
#include "B.h"
#include "C.h"
int main() {B obj1; obj1.start(); C obj2; obj2.start(); return 0;}
//------------------------------------------- class B header
class B {public: void start(); void common(); void finish();};