开发者

Reserved class name Visual C++

I want to create in my project class "Screen", "Graphics", "Font" and "Environment", but when I type these names and hit enter I get message: "Screen" is reserved class name or "Environment" is reserve开发者_开发问答d class name. I don't want to make CScreen or ScreenC or something like that :/ I have Visual Stuido 2010. Is there any way to fix this ?


I think using namespace is the answer.


This is a bug in Visual Studio. There are a number of names that Microsoft's VS 2010 product believes are reserved, because other products by Microsoft use that name. However, you are obviously not forced to use those other Microsoft products, and since these are not c++ reserved names, this is a bug and should be fixed. It should only report this as name conflicts when you include something that already uses the name. I have also seen the compiler do this for "Module" on a pure c++ project (no MFC, ATL, etc.).

In the meantime, use the Add New Item option and create the header and implementation by hand. Others have mentioned that names are important, which is true, but I cannot understand why they would be using that to dissuade you from using names like Screen, Graphics, Font, etc. Good names should be easy to use, and those are perfectly descriptive. Definitely do not try to obscure the meaning by looking up uncommon words in the thesaurus. Also, do not try to add prefixes to your class names - namespaces are the proper tool when you are making a library that will be reused by others. Prefixes were the old tool used 15 years ago when namespaces weren't added to the language yet.


As others have mentioned, this is Visual Studio preventing you from using the name, for no good reason.

The easiest way to get around this limitation, while still using the "Add -> Class..." dialog is to

  1. Type a slightly different class name in the "Class Name:" box (say "Screen_" instead of "Screen")
  2. Manually edit the name of the files under ".h file:" and ".cpp file:" in the dialog so they are Screen.h and Screen.cpp
  3. Click "Finish" to create the class files (the file names will be correct, but the actual class will be Screen_)
  4. Manually edit the text in the files, to change "Screen_" to "Screen"


What do you mean by "I type these names and hit enter"? Is this Class Wizard message? In this case just create h and cpp files manually. I don't know any C++ feature that doesn't allow to use these names for a class. Of course, in the case of name collision, use namespace.


You either need to completely not #include these classes into your project or make a namespace for your classes and then use that namespace when you need to resolve.

If you want other programmers to use your code, they might get confused that you are reusing names of classes that they already know, so it would be better to come up with a prefix for your classes, perhaps signifying the project name.


I find solution: I can add separately Graphics.cpp and Graphics.h, Environment.cpp and .. etc :/ But I still don't know why I can't use "add class" :/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜