Use of Name::Class1 from Name::Class2 in the same Name.h file fails
I've a header file 'Custom.h' with two classes, ResizeLabel and ResizePanel, used to build a dll containing Custom Controls. If I use Custom::ResizePanel within ResizeLabel it is failing:
error C2039: 'ResizePanel' : is not a member of 'Custom'
There is also a warning in the Errorlist:
Exception of type 'System.Exception' was thrown
I imagine that the warning is relevant. Could it be because Visual Studio is trying to load the dll which contains Custom::ResizePanel from the code it is compiling 开发者_如何学JAVAwhich contains it?
The code is as follows:
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
{ ... };
}
I made it a dynamic_cast just to reduce the number of errors reported.
How do I best avoid this problem?
This is classic C++ behavior. Trying to learn C++/CLI without first learning basics of standard C++ is going to be very difficult.
The general pattern to make this work is:
- Forward declare types
- Define types
- Define type member functions
in that order.
For example:
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;
}
The compile error is because ResizePanel hasn't been seen yet in the namespace. The compiler doesn't realize you will add it later. Perhaps you can change the order?
The other error might be because the dynamic_cast fails if the ResizeLabel object isn't also a ResizePanel. Can it be both at the same time?
精彩评论