1

I have the following declaration in my code:

typedef QString                          String;

Then in another header I do:

class MyClass {
  typedef String String;
};

and the following error occurs:

error: changes meaning of 'String' from 'typedef class QString String' [-fpermissive]

What is wrong with using this redeclaration?

user14416
  • 2,922
  • 5
  • 40
  • 67

1 Answers1

2

Because of the way that type aliases work, it looks to your compiler like you're trying to define MyClass::String inside of itself. It gets all confused.

[C++11: 7.1.3/6]: In a given scope, a typedef specifier shall not be used to redefine the name of any type declared in that scope to refer to a different type. [..]

Here's a complete example:

typedef int alias_t;

class T
{
  typedef alias_t alias_t;
};

Output:

test.cpp:4: error: declaration of 'typedef alias_t T::alias_t'
test.cpp:1: error: changes meaning of 'alias_t' from 'typedef int alias_t'


I can fix this example by adding the :: prefix to the existing type:

typedef int alias_t;

class T
{
  typedef ::alias_t alias_t;
};

In your code, that translates to the following:

class MyClass
{
   typedef ::String String;
};
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055