0

In c++ when I try to compile the following code I get a conflicting declaration error:

#include <iostream>
using namespace std;

typedef uint_least64_t uint;

int main() {
    uint i = -1;
    cout << i << endl;
}

error:

main.cpp:5:24: error: conflicting declaration ‘typedef uint_least64_t uint’
 typedef uint_least64_t uint;
                        ^~~~
In file included from /usr/include/stdlib.h:275:0,
                 from /usr/include/c++/6/cstdlib:75,
                 from /usr/include/c++/6/ext/string_conversions.h:41,
                 from /usr/include/c++/6/bits/basic_string.h:5417,
                 from /usr/include/c++/6/string:52,
                 from /usr/include/c++/6/bits/locale_classes.h:40,
                 from /usr/include/c++/6/bits/ios_base.h:41,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from main.cpp:1:
/usr/include/x86_64-linux-gnu/sys/types.h:152:22: note: previous declaration as ‘typedef unsigned int uint’
 typedef unsigned int uint;
                      ^~~~

I assume that conflicting declaration error is because a type declaration for uint already exists somewhere in the language, and I believe that the type is uint_least32_t because:

#include <iostream>
using namespace std;

int main() {
    uint i = -1;
    cout << i << endl;
}

returns an integer value that is (2^32)-1. Therefore is it possible in c++ to redefine uint as uint_least64_t.

asdf
  • 460
  • 1
  • 10
  • 31

3 Answers3

2

As you can see from the error message, on your platform <iostream> pulls in <string>, which pulls in <cstdlib>, which pulls in <stdlib.h>, which pulls in <sys/types.h>.

Looking at the source code, we can see that <sys/types.h> does this:

#ifdef __USE_MISC
/* Old compatibility names for C types.  */
typedef unsigned long int ulong;
typedef unsigned short int ushort;
typedef unsigned int uint;
#endif

The #ifdef __USE_MISC guard is part of the feature_test_macros system. __USE_MISC is set internally if you request _DEFAULT_SOURCE (which is also set if you request nothing else).

Thus you should be able to bypass the problem by compiling with -ansi or one of the -std=... options (e.g. -std=c++11).

melpomene
  • 84,125
  • 8
  • 85
  • 148
0

uint is not part of standard C++.

It's quite possible that something in your codebase is introducing it, or perhaps even the C++ standard library that ships with your compiler. You might be lucky and the library has introduced std::uint and you're introducing that into the global namespace when you write that polluting statement using namespace std;.

Sadly, you can't un-typedef a type or hide a class once it's been introduced.

So you'll just have to use a different symbol.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

You can not redefine the typedef symbol and will have to use different(unused) symbol.

If you want to use the same symbol you can wrap it in namespace as mentioned in this post: Using a typedef'd uint causes error, while "unsigned int" does not...?

nyemul
  • 71
  • 5