3

Visual C++ 2010 では、C++/CLI DLL ( ControlWrapper.dll ) から別の C++/CLI DLL ( CliLibrary.dll ) への参照を追加しました。

どちらもstdafx.hにafxwinforms.hを含めています。

コンパイルしようとすると、次のエラーが発生します。

error C2011: 'Microsoft::VisualC::MFC::CWin32Window' : 'class' type redefinition
error C2011: 'Microsoft::VisualC::MFC::CWinFormsEventsHelper' : 'class' type redefinition

オプション参照アセンブリ出力#using "CliLibrary.dll"をオフにして、using .cpp ファイルに追加すると、次の警告が表示されます。

1>ControlWrapper.dll : warning C4944: 'CWin32Window' : cannot import symbol from 'c:\dev\trunk\CliLibrary.dll': as 'Microsoft::VisualC::MFC::CWin32Window' already exists in the current scope
1>     C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwinforms.h(83) : see declaration of 'Microsoft::VisualC::MFC::CWin32Window'
1>          This diagnostic occurred while importing type 'Microsoft.VisualC.MFC.CWin32Window' from assembly 'CliLibrary, Version=1.0.4843.17337, Culture=neutral, PublicKeyToken=null'.
1>ControlWrapper.dll : warning C4944: 'CWinFormsEventsHelper' : cannot import symbol from 'c:\dev\sfirm\trunk\sfclrlib\debug\sfclrlib.dll': as 'Microsoft::VisualC::MFC::CWinFormsEventsHelper' already exists in the current scope
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwinforms.h(122) : see declaration of 'Microsoft::VisualC::MFC::CWinFormsEventsHelper'
1>          This diagnostic occurred while importing type 'Microsoft.VisualC.MFC.CWinFormsEventsHelper' from assembly 'CliLibrary, Version=1.0.4843.17337, Culture=neutral, PublicKeyToken=null'.

どうすればエラーを解決できますか?

4

2 に答える 2

2

うーん、これは痛い問題です。これは確かに、あなたがこれを実際に使用する最初のプログラマーである理由を説明しています。この問題は、afxwinforms.h の次の宣言が原因で発生します。

public ref class CWin32Window : public System::Windows::Forms::IWin32Window
// etc..

publicキーワードは、クラスをアセンブリのマニフェストに追加するキラーです。したがって、ヘッダーも含む別のプロジェクトでそれを参照すると、クラスの定義が 2 つあります。そのヘッダーにネイティブ クラスとマネージ クラスの両方が混在していると、クリーンなソリューションが妨げられます。

#include と #pragma comment(disable:4944) を使用して、コンパイラをシャットダウンする最適なソリューションを既に見つけたと思います。名前空間内にヘッダーを含めることは、別の実行可能なハックである可能性があります。これは、CWin32Window の名前空間の名前を変更しますが、mfcm90.lib をリンクするときに問題が発生することが予想されます。ソリューションを再構築し、すべての winforms コードを 1 つのプロジェクト内に保持することをお勧めします。

于 2013-04-05T09:46:44.680 に答える
0

afxwinforms.h の型を使用していますか?

そうでない場合 (私の場合のように)、解決策は、インクルードをコメントして、次の 2 行を追加することです。

#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

または、その 2 つのアセンブリへの参照をプロジェクトに追加します。

于 2013-05-22T11:10:50.217 に答える