4

I imported some code into my project, but there is a line: typedef signed char byte;

But the byte has been typedef in rpcndr.h. There are many codes using this type, so just changing the name is a difficult thing. I'm wondering if there's an easy way to solve such redefinition?

P.S. My code is running on Windows and all codes do not has a namespace, so add namespaces is equal to change type name.

zzy
  • 1,771
  • 1
  • 13
  • 48
  • 5
    Maybe look into namespaces? – marsh Jul 15 '15 at 01:16
  • In my experience `byte` or `BYTE` is almost always used for an unsigned char – M.M Jul 15 '15 at 01:20
  • @MattMcNabb It is used in `unpack200`, so I do not know whether it will work if I change it into unsigned char . – zzy Jul 15 '15 at 01:22
  • Ok, so as far as I understand you have two separate libraries that both declare a byte typedef? – marsh Jul 15 '15 at 01:22
  • @marsh Yes , before the custom typedef , there is `#include #include `, I don't know how to manage the include to sovle redefinition. – zzy Jul 15 '15 at 01:24
  • @MattMcNabb Making it a typedef of a plain `char` allows it to be used with standard C and C++ functions that operate on raw bytes. – Emil Laine Jul 15 '15 at 01:25
  • 1
    Are you sure you need both of those files? Try putting #define WIN32_LEAN_AND_MEAN before your includes. – marsh Jul 15 '15 at 01:26
  • @marsh that's it ! I just forget this define ! Plz post your answer I will accept it . – zzy Jul 15 '15 at 01:28
  • Glad it worked out for you. I posted it. – marsh Jul 15 '15 at 01:30
  • @zenith there's not much point in typedefing plain `char`, just use `char` – M.M Jul 15 '15 at 01:31
  • @MattMcNabb except that it conveys the semantics better: are we working on characters, or raw memory. – Emil Laine Jul 15 '15 at 01:42
  • @zenith I'd strongly recommend using `unsigned char` for working with raw memory, in order to avoid value transformations related to signed integers – M.M Jul 15 '15 at 01:50
  • @MattMcNabb Actually, C++ guarantees that [no such value transformations will happen with plain `char`](http://stackoverflow.com/a/8386615/3425536), even if it's signed. They're only allowed on `signed char`. – Emil Laine Jul 15 '15 at 03:17
  • @zenith I think C++11 and later force 2's complement anyway. I also was thinking of operations like `(x << 8) | y` . With unsigned char this combines two bytes; however if `y` is a negative plain `char` then that operation returns `y` because of "sign extension". – M.M Jul 15 '15 at 04:16

2 Answers2

4

Try adding: #define WIN32_LEAN_AND_MEAN before your includes.

What does #defining WIN32_LEAN_AND_MEAN exclude exactly?

Community
  • 1
  • 1
marsh
  • 2,592
  • 5
  • 29
  • 53
3

You could wrap your typedefs in namespaces.

namespace Foo
{
    typedef signed char byte;
}

namespace Bar
{
    typedef signed char byte;
}

Then use it like Foo::byte; Bar::byte;

Why and how should I use namespaces in C++?

Community
  • 1
  • 1
marsh
  • 2,592
  • 5
  • 29
  • 53
  • It is same to change all the byte used in code to the one with namspaces. – zzy Jul 15 '15 at 01:19
  • I am not sure what you mean, you do not need to namespace the other typedef if you cant modify there code. Simply typedef your own, so that you can access it with namespace::typedef – marsh Jul 15 '15 at 01:20