#include <iostream>
namespace
{
int a=1;
}
int a=2,b=3;
int main(void)
{
std::cout<<::a<<::b;
return 0;
}
I complie it with my g++,but the output is 23,
who can explain it?
is that a way to get access to the <unnamed> namespace ::a?
:: in ::a refers to the global namespace. Anonymous namespace should be accessed via just a (or to be more specific, you shouldn't do like this at all)
No, you can't. You can work around it thus:
namespace
{
namespace xxx
{
int a = 1;
}
}
...
std::cout << xxx::a << ::b;
Using unnamed namespaces, this is not possible. Refer the below article
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/unnamed_namespaces.htm
You have to go for named namespaces.
You can access the global namespace, but don't redefine it.
#include <iostream>
namespace
{
int a=1;
}
int b=3;
int main(void)
{
std::cout<<::a<<::b;
return 0;
}
here the out is 13.
Related
i have a few headers with namespaces which all follow a certain namepattern
right now they all have a prefix infront of their actual name such as
namespace Xname{//inside name.h
//stuff here
};
namespace Xsomething{//inside something.h
//stuff here
};
now this works pretty good for my usage atm, but my idea is to create one more header that contains a namespace that would collect all the other namespaces so i can access them like so:
#include "mainheader.h"
X::name::stuff
X::something::stuff
this way i would just change the namespace name for new headers in the future like so
X::name::stuff
hello::name::stuff
i cant nest them like this:
namespace x{
namespace something{
//stuff
}
}
You could define the new namespaces inside each other like so
#include <iostream>
namespace Xname{//inside name.h
const int x = 0;
};
namespace Xsomething{//inside something.h
const int y = 1;
};
// Your other header
namespace X {
namespace name = Xname;
namespace something = Xsomething;
}
int main() {
std::cout << X::name::x << "\n";
std::cout << X::something::y << "\n";
}
If I understand you correctly, you can use the using namespace directive for this. Here is an example:
namespace Xname {
struct foo {};
}
namespace X {
namespace name {
using namespace Xname;
}
}
int main()
{
Xname::foo f1; // original
X::name::foo f2; // alternative
}
I am trying to work with multi-dimensional arrays.
My goal is to have a separate file for my matrix functions, however I am having trouble with setting the value of V.
Error : āVā was not declared in this scope
Since this error statement is very broad, I could not find a satisfactory answer on my searches.
This is what I want to implement.
main.cpp
#include <bits/stdc++.h>
using namespace std;
#include "prims.h"
int main()
{ int V = 5;
int graph[V][V] = { {... },
{... },
{... },
{... },
{... } };
func1(graph);
func2(graph);
return 0;
}
prims.h
#ifndef PRIMS_H
#define PRIMS_H
#include <bits/stdc++.h>
using namespace std;
int func1(int graph[V][V]);
int func2(int graph[V][V]);
#endif
prims.cpp
#include <bits/stdc++.h>
using namespace std;
#include "prims.h"
int func1(int graph[V][V])
{
// function
}
int func2(int graph[V][V])
{
// function
}
Please comment below if more clarification is required.
Thank you.
Since you want to set the value from main, one alternative is to declare V as global variable in main and as extern const int in prims.h, so that it is visible in prmis.cpp as well.
prims.h
extern const int V;
main.cpp
const int V = 5; //declared as global in main
int main()
{
/* */
}
I'm trying to understand why this program does not give an name-lookup ambiguity for i:
namespace X { int i = 1; }
namespace Q {
namespace P {
int i = 2;
using namespace X;
}
using namespace P;
int l = i;
}
int main() {}
If we modify it like this we get a name-lookup ambiguity:
namespace X { int i = 1; }
namespace P {
int i = 2;
using namespace X;
}
using namespace P;
int l = i;
int main() {}
The only change I made here is to remove the namespace Q and place it's content in the global namespace instead.
I have tried with 3 different compilers:
GCC and Clang with http://melpon.org/wandbox
visual c++ with http://webcompiler.cloudapp.net/
The all give the results stated in this email, and i'm trying to find out why.
Can anyone explain the behaviour in terms of the c++ standard? I fail to understand it.
In the first program used variable i is defined in namespace P because the using directive
using namespace X;
places declarations of X in the global namespace (the common namepsace for X and P). Thus the declaration of i in P (more precisely in Q due to another using directive) hides the declaration of X::i in the global namespace.
From the C++ Standard (3.4.1 Unqualified name lookup)
2 The declarations from the namespace nominated by a using-directive
become visible in a namespace enclosing the using-directive; see
7.3.4.
So we have for the first program
namespace X { int i = 1; }
namespace Q {
namespace P {
int i = 2;
using namespace X; // 1
}
using namespace P; // 2
int l = i;
}
that the enclosing namespace for using directive #1 is the global namespace and the enclosing namespace for using directive #2 is the namepsace Q.
In the second program the both definitions of i are placed in the global namespace due to these two using directives
//...
using namespace X;
//...
using namespace P;
This question already has answers here:
How do you properly use namespaces in C++?
(16 answers)
Closed 9 years ago.
Generally when somebody creates a console program he writes
#include <iostream>
#include <stdlib.h>
int main(){
std::cout<<"hello world"<<std::endl;
system("pause");
}
The std must be included to call cout and endl statements.
When I create a library using the headers and the code in the .h and .cpp, and then I include that library, I must use the name of functions/clases/structs/etc directly. How can I make it so I have to use a pre-word like the std for cout and endl?
It's called a namespace.
You can declare your own stuff inside a namespace like this:
namespace mystuff
{
int foo();
}
To define:
int mystuff::foo()
{
return 42;
}
To use:
int bar = mystuff::foo();
Or, import a namespace, just like you can do with std if you don't want to fully-qualify everything:
using namespace mystuff;
// ...
int bar = foo();
you must define namespace like this
namespace mynamespace {
class A{
int func(){
}
}
void func2(){}
}
you can import namespace like this
using namespace mynamespace;
STD prefix is a namespace.
to define/declare a namespace you can follow that example:
namespace test
{ int f(); };
f belong to the namspace test.
to call f you can
test::f();
or
using namespace test;
....
f();
if I define a namespace log somewhere and make it accessible in the global scope, this will clash with double log(double) from the standard cmath header. Actually, most compilers seem to go along with it -- most versions of SunCC, MSVC, GCC -- but GCC 4.1.2 doesn't.
Unfortunately, there seems no way to resolve the ambiguity, as using declarations are not legal for namespace identifiers. Do you know any way I could write log::Log in the global namespace even if cmath is included?
Thanks.
EDIT: Would anybody know what the C++03 standard has to say about this? I would have thought that the scope operator sufficiently disambiguates the use of log in the code example below.
#include <cmath>
namespace foo
{
namespace log
{
struct Log { };
} // namespace log
} // namespace foo
using namespace foo;
int main()
{
log::Log x;
return 0;
}
// g++ (GCC) 4.1.2 20070115 (SUSE Linux)
// log.cpp: In function `int main()':
// log.cpp:20: error: reference to `log' is ambiguous
// /usr/include/bits/mathcalls.h:110: error: candidates are: double log(double)
// log.cpp:7: error: namespace foo::log { }
// log.cpp:20: error: expected `;' before `x'
I'd suggest:
foo::log::Log x; // Your logging class
::log(0.0); // Log function
Generally I wouldn't write using namespace foo; as there is no point having it in the foo namespace if you're not going to use it and it pollutes the global namespace.
See this related question:
How do you properly use namespaces in C++?
Although it does not help you, the error from GCC 4.1.2 is incorrect. The log in log::Log can only refer to a class or namespace name.
If your code also needs to compile with GCC 4.1.2, then there are two options:
Use the fully qualified name foo::log::Log
Use a namespace alias:
namespace log1 = foo::log;
log1::Log logger;
cmath uses ::log for some reason to get it from the global scope and can't decide between the function and your namespace.
Namespaces keep code contained to prevent confusion and pollution of function signatures.
Here's a complete and documented demo of proper namespace usage:
#include <iostream>
#include <cmath> // Uses ::log, which would be the log() here if it were not in a namespace, see https://stackoverflow.com/questions/11892976/why-is-my-log-in-the-std-namespace
// Silently overrides std::log
//double log(double d) { return 420; }
namespace uniquename {
using namespace std; // So we don't have to waste space on std:: when not needed.
double log(double d) {
return 42;
}
int main() {
cout << "Our log: " << log(4.2) << endl;
cout << "Standard log: " << std::log(4.2);
return 0;
}
}
// Global wrapper for our contained code.
int main() {
return uniquename::main();
}
Output:
Our log: 42
Standard log: 1.43508
In c++0x or c++11, the following should work, compile with -std=c++0x or -std=c++11:
#include <iostream>
#include <math.h>
namespace ___ {
namespace log {
void init() {
std::cout << "log::init()" << std::endl;
}
} // namespace log
} // namespace ___
using namespace ___;
int main() {
log::init();
std::cout << ::log(3.2) << std::endl;
return 0;
}