代码之家  ›  专栏  ›  技术社区  ›  NaturalDemon

将代码示例从c#转换为c++

  •  0
  • NaturalDemon  · 技术社区  · 12 年前

    我正在努力从下面的例子中翻译以下内容 SplitContainer Splitter Gets Focus Rectangle?

    private: Control getFocused(Control::ControlCollection controls)
    {
        //foreach (Control c in controls)
        //{
        //  if (c.Focused)
        //  {
        //      // Return the focused control
        //      return c;
        //  }
        //  else if (c.ContainsFocus)
        //  {
        //      // If the focus is contained inside a control's children
        //      // return the child
        //      return getFocused(c.Controls);
        //  }
        //}
        do
        {
                if (c.Focused)
                {
                    // Return the focused control
                    return c;
                }
                else if (c.ContainsFocus)
                {
                    // If the focus is contained inside a control's children
                    // return the child
                    return getFocused(c.Controls);
                }
        }
        while(Control c in controls);
       // No control on the form has focus
       return null;
    }
    

    我正在为DO WHILE循环寻找合适的合成器

    while(Control c in controls); // error
    

    并且由于函数' private: Control getFocused(Control::ControlCollection controls) '属于Control类型,我需要指定一个返回值,两者都有' return null; '和' return nullptr; “失败!

    编辑:

    for(int index = 0; index <= controls.Count; index++)
            {
                if(controls[index]->Focused)
                {
                    return controls[index];
                }
                else if (controls[index]->ContainsFocus)
                {
                    return getFocused(controls[index]->Controls);
                }
            }
    

    return controls[index]; -> 不存在从“System::Windows::Forms::Control^”到“System::Windows::Forms::Control”的合适的用户定义转换。

    return getFocused(controls[index]->Controls); -> 函数“getFocused”不能用给定的参数列表调用。参数类型为:(System::Windows::Forms::Control::ControlCollection^)

    返回null; -> 标识符“null”未定义

    返回nullptr; -> 不存在从“decltype(nullptr)”到“System::Windows::Forms::Control”的合适的用户定义转换

    2 回复  |  直到 12 年前
        1
  •  1
  •   Adam Maras    12 年前

    我的C++/CLI有点生疏。但让我试一试:

    private: Control ^ getFocused(Control::ControlCollection ^controls)
    {
        for each (Control ^c in controls)
        {
            if (c->Focused)
            {
                return c;
            }
            else if (c->ContainsFocus)
            {
                return getFocused(c->Controls);
            }
        }
    
        return nullptr;
    }
    
        2
  •  0
  •   Aron    12 年前

    MD.Unincorn在技术上是正确的,因为“foreach”没有C++语法。然而,C++确实在STL中包含了一些非常相似的东西(想想.net框架,而不是C#本身)。

    C++ STL for_each

    for_each(foo.begin(), foo.end(), [=](Control control){
        //Stuff
    });