Trouble with namespaces

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;
}


..
 
R

Rolf Magnus

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.

What does "nothing seems to work" mean? From the statement below, I assume
it doesn't compile. Which error does the compiler give you? In which line?
One thing that I noticed is that you have a circular header dependancy.
[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;
}


.
 
N

nan.li.g

First, you need to use forward reference to break the header mutual
inclusion cycle.
Second, you forgot to implement Class1 and Class2 constructors.

Below is the modified code and my testing enviroment

$ cat Class1.h Class2.h Driver.cpp
/////////////////////////// Class1.h //////////////////////////
#ifndef _CLASS1_H_
#define _CLASS1_H_

#include "Class2.h"

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>

namespace NS1
{
class Class1;
}

namespace NS2
{
class Class2
{
friend class NS1::Class1;

public:
Class2(){}
virtual ~Class2(){}

protected:
void confidential() {std::cout << "I am
confidential\n";}

};

}

#endif //_CLASS2_H_
#include "Class1.h"
#include "Class2.h"

using namespace NS1;
using namespace NS2;

int
main()
{
Class1 x;
Class2 y;
}
$ g++ Driver.cpp
$ g++ -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk
--host=i386-redhat-linux
Thread model: posix
gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)
$ uname -srvm
Linux 2.6.9-1.667smp #1 SMP Tue Nov 2 14:59:52 EST 2004 i686
 
N

nan.li.g

In 'First, you need to use forward reference to break the header mutual
inclusion cycle.', I should say 'forward declaration' instead. Sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top