Question

I have inherited a solution that has a C++/CLI wrapper around a C API DLL and a C# GUI. The C++/CLI wrapper was originally created with VisualStudio 2010, the GUI and the Solution where created with Visual Studio 2012. The C++/CLI Project was not upgraded at that time. In the C++/CLI project there is an enumeration defined something like this:

enum class DataCommands
{
  Read,
  Write
};

Later in the code I am logging which command was invoked. To do so I create a string like this:

String::Format("Data Command {0}", Command.ToString() );

This all worked fine until I recently tried to change the target for the C++/CLI project to be 2012 (v110). Then when I try and compile the project I get the error:

Error   1   error C2228: left of '.ToString' must have class/struct/union   C:\Path\To\File\FileName.cpp    51  1   WrapperProject

I attempted replacing the . with a ->, but that does not work either. Not sure how to resolve this, or why it changed. Happy to do the reading if you can point me to the right resource.

Thanks in Advance

Was it helpful?

Solution

Yes, that's indeed a problem in VS2012 and up. Some of the syntax that's specific to C++/CLI got adopted in the new C++11 language specification. Examples are override, nullptr and enum class. The compiler can no longer tell the difference between a managed enum and a native enum and, unfortunately, defaults to the native flavor even though /clr is in effect.

The workaround is described in this feedback article, add the access specifier to force a mismatch with valid native syntax:

private enum class DataCommands
{
  Read,
  Write
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top