4

My previous questions concerning the same project: one and two. It's not necessary to read them; just know that I am trying to use a native C++ SDK in a Visual C++ project. This is much trickier than I had initially thought, but this website about Extending a native C++ project with managed code has helped me a lot further already.

As per that last link's instructions, I have added a Form to my native C++ project, which has automatically converted the project to a CLR one. Only MainForm.cpp and Interface.cpp (the file that allows native C++ code to create and show a MainForm) are compiled with the /clr flag though; the other files remain native.

The problem I have now, is that Visual Studio doesn't seem to recognise any of the CLR stuff that's being used in MainForm.h. As such, in the following lines:

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

the word System is always underlined in red, with according errors:

error C2653: 'System' is not a class or a namespace name

for each of those lines.

It also does not recognise the word gcnew and other things that should work effortlessly inside CLR.

Can anybody tell me what I might be doing wrong? My guess is that it's something very small; some flag I have forgotten to change, a missing reference or something similar, but I just can't figure out what it is.

Community
  • 1
  • 1
Lee White
  • 3,649
  • 8
  • 37
  • 62
  • 1
    Where do you include `MainForm.h`? If you include it in any translation unit (i.e. \*.cpp) that is *not* compiled with `/clr`, then the compiler will of course complain about it, because `namespace System` and `gcnew` are not part of standard C++. – Arne Mertz Apr 02 '13 at 11:43
  • I include it in `Interface.h`. `Interface.cpp` is compiled with `/clr`, but as that third link in my post says, that should work. – Lee White Apr 02 '13 at 11:46
  • 1
    Ok, where do you include `Interface.h`? Since you include `MainForm.h` in `Interface.h`, you include it indirectly anywhere where you include `Interface.h` as well. Maybe you should only include it in `Interface.cpp` and use forward declarations in `Interface.h` – Arne Mertz Apr 02 '13 at 11:49
  • Oh wow, that was it! I was including the `MainForm.h` in `Interface.h` instead of `Interface.cpp`. This isn't mentioned in the tutorial, but it's logical when you think about it... Thanks a lot. Please post this as an answer so that I can accept and upvote it. ;) – Lee White Apr 02 '13 at 11:54

1 Answers1

4

Where do you include MainForm.h - directly and indirectly?
If you include MainForm.h in Interface.h, you include it indirectly anywhere where you include Interface.h as well. That means, if you then include Interface.h in any translation unit (i.e. *.cpp) that is not compiled with /clr, then the compiler will of course complain about it, because namespace System and gcnew are not part of standard C++.

Therefore you should include MainForm.h only in Interface.cpp and use forward declarations in Interface.h.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90