2011-02-26 40 views
1

我有一個頭文件'Custom.h'有兩個類,ResizeLabel和ResizePanel,用於構建一個包含自定義控件的dll。如果我使用內ResizeLabel定製:: ResizePanel它失敗:使用Name :: Class1從Name :: Class2在同一個Name.h文件中失敗

error C2039: 'ResizePanel' : is not a member of 'Custom' 

還有在Errorlist警告:

Exception of type 'System.Exception' was thrown 

我想象的警告是相關的。難道是因爲Visual Studio試圖從包含它的代碼中加載包含Custom :: ResizePanel的dll文件嗎?

的代碼如下:

namespace Custom { 

public ref class ResizeLabel : public System::Windows::Forms::Label 
{ 
protected: virtual void OnTextChanged(System::EventArgs^ e) override { 
      __super::OnTextChanged(e); 
      // Not elegant I know, 
      // but this is just to force the panel to process the size change 
      dynamic_cast<Custom::ResizePanel^>(this->Parent)->CurrentWidth = 0; 
     } 
    ... 
    }; 
public ref class ResizePanel : public System::Windows::Forms::Panel 
{ ... }; 
} 

我做了它的dynamic_cast只是爲了減少報告的錯誤的數量。

我該如何最好地避免此問題?

回答

2

這是經典的C++行爲。如果不先學習標準C++的基礎知識,試圖學習C++/CLI將會非常困難。

一般模式來完成這項工作是:

  • 正向聲明類型
  • 定義類型
  • 定義類型的成員函數
依次

例如:

ref class ResizeLabel; 
ref class ResizePanel; 

public ref class ResizeLabel : public System::Windows::Forms::Label 
{ 
protected: 
    virtual void OnTextChanged(System::EventArgs^ e) override; 
    ... 
}; 

public ref class ResizePanel : public System::Windows::Forms::Panel 
{ 
    ... 
}; 

void ResizeLabel::OnTextChanged(System::EventArgs^ e) 
{ 
    __super::OnTextChanged(e); 
    // Not elegant I know, 
    // but this is just to force the panel to process the size change 
    dynamic_cast<Custom::ResizePanel^>(this->Parent)->CurrentWidth = 0; 
} 
+0

你原則上是正確的,但編譯器反對在類之外的覆蓋 – bobinski 2011-02-27 08:12:19

+0

糟糕,按下輸入太快 - 我期待這個多線盒去下一行!我習慣於比這更好的編譯器,它應該解析文件兩次!最後一個函數應該讀取'void ResizeLabel :: OnTextChanged(System :: EventArgs^e) {_0super :: OnTextChanged(e); // Not elegant我知道, //但這只是強制面板處理尺寸變化 dynamic_cast (this-> Parent) - > CurrentWidth = 0; }' – bobinski 2011-02-27 08:15:46

+0

Thanks @bobinski,fixed – 2011-02-27 17:02:39

0

編譯錯誤是因爲在命名空間中還沒有看到ResizePanel。編譯器沒有意識到你會稍後添加它。也許你可以改變順序?

另一個錯誤可能是因爲如果ResizeLabel對象不是ResizePanel,則dynamic_cast失敗。它可以在同一時間嗎?

+0

你可能是正確的。不幸的是我需要兩個方向的參考。我已經將ResizePanel的參考用於ResizeLabel。我想我會交換錯誤。 – bobinski 2011-02-26 18:12:28

相關問題