質問

I have a problem with C++ namespaces

I want to have a vector shortcut for iterator of some framework. This works totally fine.

using namespace std;
using namespace GS;

typedef vector<vector<String> >::const_iterator table_iter;

However, this yields "Error: type name now allowed."

//using namespace std;
using namespace GS;

typedef vector<vector<String> >::const_iterator table_iter;

The String is actually a GS::String and I don't want to use using namespace xxx in my header file. So I thought this should work

//using namespace std;
//using namespace GS;

typedef vector<vector<GS::String> >::const_iterator table_iter;

Which also yields "Error: type name now allowed."

Obviously, I can do

using namespace std;
//using namespace GS;

typedef vector<vector<GS::String> >::const_iterator table_iter;

But why do I need the std? I think that namespace GS has some macros about using std.

Is there any way to do something like this?

typedef vector<vector<GS(YeahImUsingStd)::String> >::const_iterator table_iter;

Thanks in advance!

EDIT: I cannot edit the GS namespace!

役に立ちましたか?

解決

vector is in namespace std, so you need prefix std:: before:

using namespace GS;
typedef std::vector<std::vector<String> >::const_iterator table_iter;

or, to avoid using namespace GS too:

typedef std::vector<std::vector<GS::String> >::const_iterator table_iter;

But, because you only need vector from the standard library, you can do that too:

using std::vector;
using namespace GS;
typedef vector<vector<String> >::const_iterator table_iter;

In this way you says to the compiler you want to use vector from std:: namespace, but not bring all namespace.

But you said you want to avoid using namespace in your header file. That is usually good idea (including using std::vector), so the best solution is the second one:

typedef std::vector<std::vector<GS::String> >::const_iterator table_iter;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top