Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forward declaration of namespace

namespace CounterNameSpace {
    int upperbound;
    int lowerbound;
    using namespace NS;//Error
}
namespace NS {
int i;
}
// ...
namespace NS {
int j;
}

In the above case it shows an error . error C2871: 'NS' : a namespace with this name does not exist I know if i define NS before counternamespace problem will be solved . But just want to know whether any thing like forward declaration of namespace exist in c++ or not .So that the above problem will be resolved without defining NS before counternamespace . please help .

like image 270
Viku Avatar asked Dec 31 '12 08:12

Viku


People also ask

Can I forward declare a namespace?

There is no forward declaration of namespace. You can forward declare a function.

What is forwarding declaration?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.

What is a forward declaration in C++?

In C++, Forward declarations are usually used for Classes. In this, the class is pre-defined before its use so that it can be called and used by other classes that are defined before this. Example: // Forward Declaration class A class A; // Definition of class A class A{ // Body };

How do you declare a namespace?

Typically, you declare a namespace in a header file. If your function implementations are in a separate file, then qualify the function names, as in this example. A namespace can be declared in multiple blocks in a single file, and in multiple files.


1 Answers

Nothing says a namespace needs all of its contents right away:

namespace NS {}
namespace CounterNameSpace {
    int upperbound;
    int lowerbound;
    using namespace NS;
}
namespace NS {
int i;
}

However, this might not do what you want. You still won't be able to use any of the types in that namespace until you've declared them.

like image 178
Cory Nelson Avatar answered Sep 24 '22 00:09

Cory Nelson