在同一 Name.h 文件中使用 Name::Class2 中的 Name::Class1 失败

发布于 2024-10-19 08:24:50 字数 1003 浏览 2 评论 0原文

我有一个头文件“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍孤女 2024-10-26 08:24:50

这是经典的 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;
}

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;
}
美男兮 2024-10-26 08:24:50

编译错误是因为命名空间中尚未看到 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文