C++: using namespace directive for non-existing namespace [duplicate] - c++

This question already has answers here:
"using namespace std;" without any #include? [duplicate]
(3 answers)
Closed 2 years ago.
I am tryting to compile following code with g++ (version 7.5.0)
using namespace nspace;
int main()
{
return 0;
}
It gives error as follow
$ g++ above_code.cpp
namespaces_mystery1.cpp:1:17: error: ‘nspace’ is not a namespace-name
using namespace nspace;
^~~~~~
namespaces_mystery1.cpp:1:23: error: expected namespace-name before ‘;’ token
using namespace nspace;
^
Above behaviour is what I have expected.
But when I try to compile following code, it compiles fine without error like above.
using namespace std;
int main()
{
return 0;
}
Why this different behaviour for namespace named std compared to namespace named nspace

The namespace nspace doesn't exist at the point using namespace nspace; is encountered, whereas the std namespace does. The latter could be true due to implicit or explicit inclusion of facets of the C++ standard library, or the compiler itself might even hardcode it.
If you had written
namespace nspace{}
before the using statement, then compilation would succeed.

Related

What is "using namespace::std" in C++

I am reading some code snippets from others and I find a line:
using namespace::std;
I suspect its purpose is using namespace std;, with some typos. But to my surprise the compiler accepts this code without any complaint. I build with:
$ g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0
$ /usr/bin/g++ ${SRC} -std=c++11 -pthread -Wall -Wno-deprecated -o ${OUT}
I wonder why is this code valid, and what effects will it make? I suspect it is a bad practice.
It's equivalent to using namespace ::std; and also equivalent to using namespace std;. The :: refers to the global namespace, and std is put in the global namespace indeed.
As the syntax of using-directives:
(emphasis mine)
attr(optional) using namespace nested-name-specifier(optional) namespace-name ;
... ...
nested-name-specifier - a sequence of names and scope resolution
operators ::, ending with a scope resolution operator. A single ::
refers to the global namespace.
... ...
using namespace::std is the same as using namespace std;
The :: symbol is the scope resolution operator. When used without a scope name before it , it refers to the global namespace. This means that std is a top level namespace, and is not enclosed in another.
The spaces before and after the :: are optional in this case because the lexer can deduce the tokens from the context.
For example, all of the following are valid:
namespace A { namespace notstd{} } // define my own namespaces A and A::notstd
using namespace::std; // the standard library std
using namespace A;
using namespace ::A;
using namespace::A;
using namespace A::notstd;
Update:
As noted in one of the comments, using namespace ::std; and using namespace std; may actually lead to different results if the statement comes inside another namespace which contains its own nested namespace std. See the following (highly unlikely) example:
#include <stdio.h>
namespace A {
namespace std {
int cout = 5;
}
using namespace std;
void f1() {
cout++;
}
}
int main()
{
A::f1();
printf("%d\n",A::std::cout); // prints 6
return 0;
}

how come an undeclared variable is outputting a value [duplicate]

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 5 years ago.
In my function sumofrange I decided to output an undeclared variable just to learn the different compiler errors in C++. To my surprise, time seems to output 1 even though it is not declared anywhere.
#include <iostream>
#include <cmath>
using namespace std;
int sumOfrange( int lower, int upper){
cout<<time<<endl;
return ((( (pow(upper,2)) + upper) - ((pow(lower,2)) + lower)) / 2);
}
int main(){
cout<<sumOfrange(7,100)<<endl;
return 0;
}
You are outputting the address of a std::time function declared in a <ctime> header. You are also using a using namespace std; statement. Why that should be avoided is explained in this SO post. Depending on the compiler and the platform you might get the hexadecimal output similar to (0x)00DC52E0 if using a VC++ compiler on Windows or a number 1 if using a g++ compiler on Linux.

'list' was not declared in this scope

I am new to c++, and I am trying to get a basic program to initialize a list of short unsigned integers. I am compiling and running using scygwin and g++.
Below is the code in the .cpp file:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <typeinfo>
using namespace std;
int main (int argc, char* argv[]) {
list<int> events;
return 0;
}
which I run by typing the following command into cygwin terminal:
$ g++ -o test.out test.cpp
However, I get the following compilation errors:
test.cpp: In function ‘int main(int, char**)’: test.cpp:16:1: error:
‘list’ was not declared in this scope list events;
^
test.cpp:16:6: error: expected primary-expression before ‘int’
list events;
^
I am confused about why list is not in the scope, since I am using namespace std? I found a similar question asked about this on a c++ forum, but my problem would be resolved with that. Anyone know what the problem is here?
-Paul
using namespace std; doesn't add any functionality to your code. It just means you don't have to type std:: when referencing things in the std namespace, like std::list.
To actually include the code base for std::list into your program, you need to add:
#include <list>
When in doubt about this kind of thing, doing a google search for cpp reference list will turn up a page like this where you can see: Defined in header <list> at the top.
Here's another question about using namespace std; that may prove useful and why you shouldn't use it. I'll add a little bit to perhaps explain namespaces.
It is common in C++ programs to organize functions into classes and namespaces. Imagine you wrote your own list class to handle certain scenarios. In order to prevent naming conflicts you would put it in a different namespace than std.
namespace MyApp {
class list;
void sort(list&);
}
For the majority of a large code base you might still prefer to use std::list but you need MyApp::list for some things. Using namespaces you can cluster your code and prevent naming conflicts for similar functionality.
Summary
using namespace std; makes it so that if you reference a function or class not in the global namespace it looks for it in the std namespace.
#include <list> actually inserts prototypes (information about how to access the code) in your source file during the preprocessor stage.

ISO C++ forbids declaration of ‘multiset’ with no type

I am getting this error while building a software (ns3) using waf
In file included from ../src/internet-stack/mp-tcp-typedefs.cc:6:
../src/internet-stack/mp-tcp-typedefs.h:151: error: ISO C++ forbids declaration of ‘multiset’ with no type
../src/internet-stack/mp-tcp-typedefs.h:151: error: expected ‘;’ before ‘<’ token
In file included from ../src/internet-stack/mp-tcp-socket-impl.cc:17:
../src/internet-stack/mp-tcp-typedefs.h:151: error: ISO C++ forbids declaration of ‘multiset’ with no type
../src/internet-stack/mp-tcp-typedefs.h:151: error: expected ‘;’ before ‘<’ token
I searched for the error and the solutions say that probably I am missing a using namespace std or #include <set> in my C++ code, but my code is not missing those. The file where the error originates [mp-tcp-typedefs.h] is here (Line 151 has the error).
I tried resolving the error but still, I Am getting those for a long time now.
My gcc/g++ version is g++ (Ubuntu/Linaro 4.4.7-8ubuntu1) 4.4.7.
You should not put using namespace std; in a header file:
Why is "using namespace std;" considered bad practice?
You can probably fix your code by moving your using namespace std; inside your own namespace changing this:
using namespace std;
namespace ns3 {
to this:
namespace ns3 {
using namespace std;
But better to remove the using namespace std; and qualify all your standard symbols with std:: or else declare them individually inside your own namespace.
namespace ns3 {
using std::string;
using std::list;
using std::multiset;
using std::queue;

"Not declared in scope" C++ issue

I'm writing a simple class in C++ for a class (school, not code). I have a little C++ experience, but it's been a while so I'm relearning whatever I forgot and learning a lot of new syntax (I have much more experience in Java). Here is the code:
#include <iostream>
#include <string>
using namespace std;
class Project112
{
private:
string romanNumeral;
int decimalForm;
public:
Project112()
{
romanNumeral = "";
decimalForm = 0;
}
int getDecimal()
{
return decimalForm;
}
};
and here is the driver:
include cstdlib
include <iostream>
using namespace std;
int main()
{
Project112 x;
int value2 = x.getDecimal();
return 0;
}
This is part of a larger program, but I've simplified it down to this because this is the where the problem lies. Every time I try to run the program, I get the following errors:
main.cpp:10: error: 'Project112' was not declared in this scope
main.cpp:10: error: expected `;' before 'x'
main.cpp:14: error: 'x' was not declared in this scope
Can someone please explain the problem? Thanks in advance.
#include "Project112.h"
Add that above main. You forgot to include the header file. And:
using namespace std;
Don't do that in a header file. That imports the everything from the std namespace into the global namespace of any file which includes your header. Just fully qualify the type in a header, i.e., std::string, and I would avoid that in implementation files as well in a large project (though something like using std::string is ok IMO in an implementation file).