如果我创建一个名为 myForm 的新表单,myForm.h 的顶部如下所示:
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections; //<<<< THIS ONE
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
甚至不需要这些,因为出色的表单设计器总是完全限定其对象。
标有 THIS ONE 的那个特别烦人,因为它破坏了我的构建。这是因为我到处都使用 IList 的通用形式——我非常喜欢它,所以我把它放在 stdafx.h 中,如下所示:
using System::Collections::Generic::IList;
那么如果我想在我碰巧使用 IList 的任何其他文件中使用 myForm,就像这样:
#include "StdAfx.h"
#include "ABC.h"
#include "myForm.h"
ABC::ABC()
{
IList<int>^ myList;
...
}
然后编译失败:
1>.\ABC.cpp(7) : error C2872: 'IList' : ambiguous symbol
1> could be 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::Generic::IList'
1> or 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::IList'
1>.\ABC.cpp(7) : error C2872: 'IList' : ambiguous symbol
1> could be 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::Generic::IList'
1> or 'c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : System::Collections::IList'
那么,我怎样才能阻止新表单添加所有这些无用和破坏性的用法呢?
最佳答案
您可以通过针对您使用的特定语言编辑 ItemTemplates 目录中的 zip 文件来更改 Visual Studio 使用的默认模板。
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033
是 C# 模板所在的位置。我假设 C++ 模板位于类似的目录中,但我手边没有安装了 C++ 的 VS 实例。
关于c++ - 如何阻止新表单使用命名空间 System::Collections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1433629/