U
usenet
I have run into trouble with namespaces. The following code snippet
illustrates the problem that I am facing:
Class1 belongs to namespace NS1 and Class2 belongs to namespace NS2. I
want to declare Class1 as a friend of Class2, and I am trying to invoke
a protected (or private) method on Class2 from Class1. I have tried a
few things, but nothing seems to work. Any help will be appreciated.
[The code compiles fine if namespaces are removed].
Thanks,
Gus
/////////////////////////// Class1.h //////////////////////////
#ifndef _CLASS1_H_
#define _CLASS1_H_
#include "Class2.h"
//using namespace NS2;
namespace NS1
{
class Class1
{
public:
Class1();
virtual ~Class1(){}
void doIt(NS2::Class2& val) {val.confidential();}
};
}
#endif //_CLASS1_H_
/////////////////////////// Class2.h //////////////////////////
#ifndef _CLASS2_H_
#define _CLASS2_H_
#include <iostream>
#include "Class1.h"
//using namespace NS1;
namespace NS2
{
class Class2
{
friend class NS1::Class1;
public:
Class2();
virtual ~Class2(){}
protected:
void confidential() {std::cout << "I am confidential\n";}
};
}
#endif //_CLASS2_H_
//////////////////Driver.cpp///////////////////
#include "Class1.h"
#include "Class2.h"
using namespace NS1;
using namespace NS2;
int
main()
{
Class1 x;
Class2 y;
}
..
illustrates the problem that I am facing:
Class1 belongs to namespace NS1 and Class2 belongs to namespace NS2. I
want to declare Class1 as a friend of Class2, and I am trying to invoke
a protected (or private) method on Class2 from Class1. I have tried a
few things, but nothing seems to work. Any help will be appreciated.
[The code compiles fine if namespaces are removed].
Thanks,
Gus
/////////////////////////// Class1.h //////////////////////////
#ifndef _CLASS1_H_
#define _CLASS1_H_
#include "Class2.h"
//using namespace NS2;
namespace NS1
{
class Class1
{
public:
Class1();
virtual ~Class1(){}
void doIt(NS2::Class2& val) {val.confidential();}
};
}
#endif //_CLASS1_H_
/////////////////////////// Class2.h //////////////////////////
#ifndef _CLASS2_H_
#define _CLASS2_H_
#include <iostream>
#include "Class1.h"
//using namespace NS1;
namespace NS2
{
class Class2
{
friend class NS1::Class1;
public:
Class2();
virtual ~Class2(){}
protected:
void confidential() {std::cout << "I am confidential\n";}
};
}
#endif //_CLASS2_H_
//////////////////Driver.cpp///////////////////
#include "Class1.h"
#include "Class2.h"
using namespace NS1;
using namespace NS2;
int
main()
{
Class1 x;
Class2 y;
}
..