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;
}
.. 3 1136 us****@sta.samsung.com wrote: 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; }
.
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
In 'First, you need to use forward reference to break the header mutual
inclusion cycle.', I should say 'forward declaration' instead. Sorry. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Lol McBride |
last post by:
Hi all,
I'm developing an OOP program and trying to be smart by using unittest to
help me to catch as many bugs as possible during development.Unfortunately
I've hit a snag which I believe is to...
|
by: Steven Bethard |
last post by:
In the "empty classes as c structs?" thread, we've been talking in some
detail about my proposed "generic objects" PEP. Based on a number of
suggestions, I'm thinking more and more that instead of...
|
by: Ney André de Mello Zunino |
last post by:
Hello.
I am having trouble with namespaces and unresolved externals. The
following three files (memoria.h, memoria.cpp and teste.cpp) illustrate
the problem:
// *************** memoria.h...
|
by: Random |
last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I
understand the purpose is so the developer can put classes within namespaces
to essentially organize your code. And I...
|
by: mlg1906 |
last post by:
I'm developing an intranet site in ASP.NET 2.0 but I can't seem to
connect to the DB from within my code. I've created a .vb class that
houses a private Connection() that other functions within the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |