chevron_left

The 'ERROR (expected nested-name-specifier before ‘namespace’)` error you may get while compiling your C++ code with a C++ compiler, this kind of issue might frustrate one, here we will discuss in the depth about the issue

Photo by Rachel Vine
TABLE OF CONTENTS
  1. What is "ERROR (expected nested-name-specifier before ‘namespace’)"?
  2. Why "ERROR (expected nested-name-specifier before ‘namespace’)" happens?
  3. How to resolve "ERROR (expected nested-name-specifier before ‘namespace’)":
    1. Solution:
  4. Conclusion

What is  “ERROR (expected nested-name-specifier before ‘namespace’)”?

Before discussing the error, we should know, what is a nested name specifier?

C++ nested name specifiers are the prefixes to qualified names. For example, “foo::” in “foo::x” is a nested name specifier. Nested name specifiers are made up of a sequence of specifiers, each of which can be namespace, type, identifier (for dependent names), decltype specifier, or the global specifier (‘::’). The last two specifiers can only appear at the start of a nested-namespace-specifier.

#include 
#include 

template
class ListPtr;

template
class ListPtrIterator {

public:
ListPtrIterator( ListPtr* _user ):
M_user( _user ),
M_cur( M_user->begin() ) {}

bool
cur( T*& );

bool
reset( void ) {
M_cur =M_user->begin();
return M_cur!=M_user->end();
}

private:
ListPtr* M_user;
typename list::iterator M_cur;
^^^^
ERROR: expected nested-name-specifier before 'list'
};

Output

new.cpp:28:46: warning: multi-character character constant [-Wmultichar]

 ERROR: expected nested-name-specifier before 'list'

Why does “ERROR (expected nested-name-specifier before ‘namespace’)“ happens?

As the compiler suggests that there is an with the list. Here the compiler does not accept the list as it is. Compiler accepts it as std::list.

As we know if we do not use the using namespace std in our program then we will face an error saying may be like that cout was not declared in this scope. But we know that in the past we did not need to define the using namespace std in the program. But now we have to use it or we have to use cout as std::cout. 

In the program given above the cause is pretty similar to this reason.

Following is the error message you get:

` main.cpp online 2:7: error: expected nested-name-specifier before ‘namespace’
 using namespace std;
       ^~~~~~~~~`

 

How to resolve “ERROR (expected nested-name-specifier before ‘namespace’)”?

Solution :

As I mentioned earlier, in the program given above the cause is pretty similar to this reason. So we have to use std::list to resolve the error since compilers do not accept containers without nested-name-specifier.

 

⚠️ Remember to use C++11 (otherwise you'll receive an error saying that extended initializer lists are only available with -std=c++11 or -std=gnu++11)..

 

ℹ   Ensure that the relevant header files are included.

Conclusion:

 We discussed in this article about ERROR (expected nested-name-specifier before ‘namespace’) discussed and a number of solutions to get around it, one final way out may be be to upgrade to a more recent compiler version 

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

Error does not name a type in C++ cafe-coder45 - Jun 2
ERROR (expression must have integral or unscoped enum type) AnkurRanpariyav - Aug 3
ERROR invalid type argument of unary '*' (have 'int') AnkurRanpariyav - Aug 3
Terminate called after throwing an instance of `char const*` AnkurRanpariyav - Aug 7
Split is not a function Error in JavaScript cafe-coder45 - Jun 1