简体   繁体   中英

Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)

I need to work on a project that was written in msvcpp6.0sp6

I DIDN'T write the project. I know very little about its inner works. I DO know it WAS POSSIBLE to build it in the past.

while trying to build this project that was built successfuly in the past(not by me)

I get the error:

        Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)

for example: error C2664: 'strncpy' : cannot convert parameter 2 from 'const unsigned short *' to 'const char *'

error C2664: 'void __cdecl CString::Format(const unsigned short *,...)' : cannot convert parameter 1

for a few dozen implicit conversions. I mustn't change the code. how can I force the complier to accept the implicit convertions?

That sounds crazy.

The use of unsigned short * with string-handling functions like strncpy() initially seems to make no sense at all. On second thought though, it makes me wonder if there is some kind of "wide character" configuration that is failing. If strncpy() was "re-targeted" by the compiler to work on 16-bit characters, having it expect unsigned short * makes sense and would explain why the code passes it such. At least "kind of" explain, it's still odd.

You can't. There are no such implicit conversions defined by the C++ language.

Visual C++ 6.0 was a law unto itself; by implementing something that merely looked a bit like the C++ language, it may have accepted this invalid code.

I mustn't change the code. how can I force the complier to accept the implicit convertions?

Quite likely you need to get the same compiler that was used for the code in the first place, and use that.

If my guess (in a comment on unwind's answer) is correct about that unsigned short* error then it's simply not possible to compile this code in Unicode mode, because the source is insufficiently portable. Suppressing the error for the conversion, even if it's possible via some compiler setting, will just result in code that compiles but doesn't work.

I'd expect that also to imply that the old dll probably isn't compatible with the rest of your current code, but if you've been using it up to now then either I'm wrong about the reason, or else you've got away with it somehow.

C++ is a typesafe language. But it allows you to tell the compiler to "shut up" by the evil known as casting.

Casting from integers to enums is often a necessary "evil" cast, for example, you cannot loop through enums where you have, say, a restricted number of values for which you have given enumerations. Therefore you have to use an integer and cast them to enums for this purpose.

Sometimes you do need to cast data structors to const char * rather than const void * just so you can perform pointer arithmetic. However for the purpose of strcpy, it is difficult to see why you want to cast in unsigned shorts. If these are wide characters (and the old compiler did not know of wchar_t) then it may be "safe" to cast it to const wchar_t * and use it in a wide-string copy. You could also use C++ strings, ie std::string and std::wstring.

If you really do not wish to update the source code for ISO compliance then your best bet is to use the original VC++ 6.0 compiler for your legacy code. Not least because even though you know this code to work, if it were compiled with a different compiler it will be different code and may no longer work. Any undefined or implementation defined compiler behaviour either exploited or used inadvertently could cause problems if a different compiler is used.

If you have an MSDN subscription, you can download all previous versions of VC++ for this purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM