在同一 Name.h 文件中使用 Name::Class2 中的 Name::Class1 失败
我有一个头文件“Custom.h”,其中包含两个类:ResizeLabel 和 ResizePanel,用于构建包含自定义控件的 dll。如果我在 ResizeLabel 中使用 Custom::ResizePanel 它会失败:
error C2039: 'ResizePanel' : is not a member of 'Custom'
错误列表中还有一个警告:
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只是为了减少报告的错误数量。
我怎样才能最好地避免这个问题?
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 which 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是经典的 C++ 行为。在不首先学习标准 C++ 基础知识的情况下尝试学习 C++/CLI 将非常困难。
完成这项工作的一般模式是:
按该顺序
。例如:
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:
in that order.
For example:
编译错误是因为命名空间中尚未看到 ResizePanel。编译器没有意识到您稍后会添加它。也许你可以改变顺序?
另一个错误可能是因为如果 ResizeLabel 对象不是 ResizePanel,则dynamic_cast 会失败。两者可以同时进行吗?
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?