473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Forward declarations and namespaces

How do I do forward declarations when namespaces are involved? For
example, given the following:

-------------------------------------ClassA.h
#ifndef CLASSA_H
#define CLASSA_H

namespace MyNameSpace
{
class A{};
};

#endif
--------------------------------------ClassB.h
#ifndef CLASSB_H
#define CLASSB_H

class A;

namespace MyNameSpace
{
class B
{
MyNameSpace::A* m_pAinst;
};

};

#endif
How do I declare A so I don't have to include the header classA.h in
classB.h

thanks
Jul 22 '05 #1
4 1433
"whithers" <wh*****@aol.com> wrote:
How do I do forward declarations when namespaces are involved?


You can just say:

namespace MyNameSpace
{
class A;
};

David F
Jul 22 '05 #2

"whithers" <wh*****@aol.com> wrote in message
news:ou********************************@4ax.com...
How do I do forward declarations when namespaces are involved? For
example, given the following:

-------------------------------------ClassA.h
#ifndef CLASSA_H
#define CLASSA_H

namespace MyNameSpace
{
class A{};
};

#endif
--------------------------------------ClassB.h
#ifndef CLASSB_H
#define CLASSB_H

class A;

namespace MyNameSpace
{
class B
{
MyNameSpace::A* m_pAinst;
};

};

#endif
How do I declare A so I don't have to include the header classA.h in
classB.h

thanks


Try this:
//B.h
#ifndef CLASSB_H
#define CLASSB_H

namespace MyNameSpace {
class A;
class B {
MyNameSpace::A* m_pAinst;
};

}

#endif

//test.cpp
#include "B.h"

int main()
{
MyNameSpace::B b;
}
Regards,
Sumit.
Jul 22 '05 #3
whithers wrote:
How do I do forward declarations when namespaces are involved?
For example, given the following:

-------------------------------------ClassA.h
#ifndef CLASSA_H
#define CLASSA_H 1

namespace MyNameSpace {
class A{};
};

#endif//CLASSA_H
--------------------------------------ClassB.h
#ifndef CLASSB_H
#define CLASSB_H 1

namespace MyNameSpace {
class A;
class B {
A* m_pAinst;
};
};

#endif//CLASSB_H
How do I declare A so that
I don't have to include the header classA.h in classB.h?


Gasp!

Jul 22 '05 #4

"Sumit Rajan" <su********@myrealbox.com> wrote in message
news:bu************@ID-206022.news.uni-berlin.de...

"whithers" <wh*****@aol.com> wrote in message
news:ou********************************@4ax.com...
How do I do forward declarations when namespaces are involved? For
example, given the following:

-------------------------------------ClassA.h
#ifndef CLASSA_H
#define CLASSA_H

namespace MyNameSpace
{
class A{};
};

#endif
--------------------------------------ClassB.h
#ifndef CLASSB_H
#define CLASSB_H

class A;

namespace MyNameSpace
{
class B
{
MyNameSpace::A* m_pAinst;
The "MyNameSpace::" part is not really neccessary in the above line -- just
"A* m_pAinst;" would do the job.
};

};

#endif
How do I declare A so I don't have to include the header classA.h in
classB.h

thanks


Try this:
//B.h
#ifndef CLASSB_H
#define CLASSB_H

namespace MyNameSpace {
class A;
class B {
MyNameSpace::A* m_pAinst;
};

}

#endif

//test.cpp
#include "B.h"

int main()
{
MyNameSpace::B b;
}
Regards,
Sumit.

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: matthew polder | last post by:
Hi, I have two libraries that used different namespaces, call them "fruit" and "vegetable." In one of the class definitions of fruit, I have the following. class corn; //This doesn't work. ...
3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
11
by: aleko | last post by:
This applies equally to function prototypes. Why do we have to tell the compiler twice? Other modern compiled languages don't have this requirement. Just curious, Aleko
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
8
by: Simon Brooke | last post by:
I was debugging a new XML generator tonight and trying to determine why it wasn't working; and realised my dom printer does not output XML namespace declarations. My method to output an Element...
1
by: ctor | last post by:
Hi, I'm experimenting with using a lot of different namespaces in my current project to see if it helps me keep my code more organized. In some ways I'm finding that it causes more problems...
10
by: collection60 | last post by:
Will C++ ever have Java-like multiple level compiling? So that we can dump the old model of having to first declare then write the code? I find the declaration wastes so much time in coding....
10
by: Andy Fish | last post by:
hi, I have an XSLT which is producing XML output. many of the nodes in the output tree contain namespace declarations for namespaces that are used in the source document even though they are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.