• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

2 classes to communicate with each other through namespace

I have a visual C++ project with multiple cpp files that have 2 classes. These 2 classes are to talk to each other. Class B is to be able to use data members from Class A without modifying each class really. The hint for this class assignment is to use namespaces. What is the trick behind this?
0
allenlo77
Asked:
allenlo77
  • 2
1 Solution
 
jkrCommented:
I'd say that you won't really need namespaces here, but if you have to, think of it like
// a.h
 
#ifndef A_H
#define A_H
 
#include "b.h"
 
namespace NA {
 
class A {
 
public:
 
A() : a(1) {}
 
void SetB(NB::B* p);
const int& GetVal() const;
void PrintValB() const;
 
protected:
 
int a;
NB::B* pb;
};
 
}; // end namespace
#endif
 
------------------------------>8------------------
 
// b.h
 
#ifndef B_H
#define B_H
 
#include "a.h"
 
namespace NB {
 
class B {
 
public:
 
B() : b(2) {}
 
void SetA(NA::A* p);
const int& GetVal() const;
void PrintValA() const;
 
protected:
 
int b;
NA::A* pa;
};
 
}; // end namespace
#endif
 
------------------------------>8------------------
 
// a.cpp
 
#include "b.h"
#include <iostream>
 
using namespace NB;
using namespace std;
 
void A::SetB(NB::B* p) {
 
  pb = p;
}
const int& A::GetVal() const {
 
  return a;
}
void PrintValB() const {
 
  cout << pb->GetVal();
}
 
------------------------------>8------------------
 
// b.cpp
 
#include "a.h"
#include <iostream>
 
using namespace NB;
using namespace std;
 
void B::SetA(NA::A* p) {
 
  pa = p;
}
const int& B::GetVal() const {
 
  return b;
}
void PrintValA() const {
 
  cout << pa->GetVal();
}
 
 
------------------------------>8------------------
 
// main.cpp
 
#include "a.h"
#include "b.h"
 
using namespace NA;
using namespace NB;
 
int main () {
 
  A a;
  B b;
 
  a.SetB(&b);
  b.SetA(&a);
 
  a.PrintValB();
  b.PrintValA();
 
  return 0;
}

Open in new window

0
 
allenlo77Author Commented:
I'm compiling your example with alot of errors. It seems that both classes can't see each other's namespaces. Why?
0
 
jkrCommented:
Sorry, my fault, typed that into the browser. I double checked the following code:


// a.h
 
#ifndef A_H
#define A_H
 
#include "b.h"
 
namespace NA {
 
class A {
 
public:
 
A() : a(1) {};
 
void SetB(NB::B* p);
const int& GetVal() const;
void PrintValB() const;
 
protected:
 
int a;
NB::B* pb;
};
 
}; // end namespace
#else
namespace NA {
class A;
};
#endif
 
 
------------------
 
// b.h
 
#ifndef B_H
#define B_H
 
#include "a.h"
 
namespace NB {
 
class B {
 
public:
 
B() : b(2) {};
 
void SetA(NA::A* p);
const int& GetVal() const;
void PrintValA() const;
 
protected:
 
int b;
NA::A* pa;
};
 
}; // end namespace
#else
namespace NB {
class B;
};
#endif
 
 
--------------------------
 
// a.cpp
 
#include "a.h"
#include <iostream>
 
using namespace NA;
using namespace std;
 
void A::SetB(NB::B* p) {
 
  pb = p;
}
const int& A::GetVal() const {
 
  return a;
}
void A::PrintValB() const {
 
  cout << pb->GetVal();
}
 
 
----------------
// b.cpp
 
#include "a.h"
#include <iostream>
 
using namespace NB;
using namespace std;
 
void B::SetA(NA::A* p) {
 
  pa = p;
}
const int& B::GetVal() const {
 
  return b;
}
void B::PrintValA() const {
 
  cout << pa->GetVal();
}
 
----------------
 
// main.cpp
 
#include "a.h"
#include "b.h"
 
using namespace NA;
using namespace NB;
 
int main () {
 
  A a;
  B b;
 
  a.SetB(&b);
  b.SetA(&a);
 
  a.PrintValB();
  b.PrintValA();
 
  return 0;
}
 

Open in new window

0
 
Infinity08Commented:
>> Class B is to be able to use data members from Class A without modifying each class really.

That sounds like a job for the 'friend' keyword ...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now