如何使用 C++ 在 bada 中调用函数/将值从一个类传递到另一个类友元函数

发布于 2024-12-21 08:47:48 字数 1576 浏览 6 评论 0原文

我知道这是一个基本的 C++ 问题,但我可以知道如何使用友元函数在 bada 中从一个类调用函数/传递值 (elementId) 到另一个类吗?

在我的表单类中,我有一个listView,当单击listView中的项目时,我想将elementId传递给detailedForm以在标签中显示信息(在detailedForm中)。在我的form.h和.cpp中,我包含了detailedForm.h,我可以知道如何访问detailedForm中的函数来显示信息吗?在 form.h 中,我还声明了

friend class detailedForm;

当我尝试在表单类中使用DetailedForm 中的函数之一时,即displayInfo();表单类出现错误,提示未声明 displayInfo()。

form.h

...
public:
    friend class ChartFormDetail;

这是我的 form.cpp 代码、

#include "Form.h"
#include "ChartFormDetail.h"
...

void
Form::OnGroupedListViewItemStateChanged(Osp::Ui::Controls::GroupedListView &listView, int groupIndex, int itemIndex, int elementId, Osp::Ui::Controls::ListItemStatus state)
{
    Frame* pFrame = Osp::App::Application::GetInstance()->GetAppFrame()->GetFrame();
    FormMgr* pFormMgr = dynamic_cast<FormMgr*> (pFrame->GetControl("FormMgr"));

    if(pFormMgr == null)
    return;

    pFormMgr->SendUserEvent(FormMgr::REQUEST_DETAILFORM, null);
    //pFormMgr->SendUserEvent(elementId, null);


    switch(elementId)
        {
        case ID_FORMAT_STRING_M12:
            DisplayLabel();
            break;
...
        case ID_FORMAT_STRING_F19:
            DisplayLabel();
            break;
        }
}

detailedForm.h

public:
...
    void DisplayLabel(void);

代码、detailedForm.cpp 代码

void
ChartFormDetail::DisplayInfo(void)
{
    pLabel->SetText("Text here");
    RequestRedraw();
}

I know this is a basic c++ question, but may I know how can I call a function/pass value (elementId) from one class to another in bada using the friend function?

In my form class, I have a listView and when the item in the listView has been clicked, I would like to pass the elementId to detailedForm to display info in the label (in detailedForm). In my form.h and .cpp I have included detailedForm.h, may I know how do I access the function in detailedForm to display the info? In form.h, I have also declared

friend class detailedForm;

and when I tried to use one of the function in detailedForm in my form class, namely displayInfo(); the form class has an error saying displayInfo() has not been declared.

form.h

...
public:
    friend class ChartFormDetail;

Here is my code for form.cpp

#include "Form.h"
#include "ChartFormDetail.h"
...

void
Form::OnGroupedListViewItemStateChanged(Osp::Ui::Controls::GroupedListView &listView, int groupIndex, int itemIndex, int elementId, Osp::Ui::Controls::ListItemStatus state)
{
    Frame* pFrame = Osp::App::Application::GetInstance()->GetAppFrame()->GetFrame();
    FormMgr* pFormMgr = dynamic_cast<FormMgr*> (pFrame->GetControl("FormMgr"));

    if(pFormMgr == null)
    return;

    pFormMgr->SendUserEvent(FormMgr::REQUEST_DETAILFORM, null);
    //pFormMgr->SendUserEvent(elementId, null);


    switch(elementId)
        {
        case ID_FORMAT_STRING_M12:
            DisplayLabel();
            break;
...
        case ID_FORMAT_STRING_F19:
            DisplayLabel();
            break;
        }
}

detailedForm.h

public:
...
    void DisplayLabel(void);

code for detailedForm.cpp

void
ChartFormDetail::DisplayInfo(void)
{
    pLabel->SetText("Text here");
    RequestRedraw();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

╰沐子 2024-12-28 08:47:48

看起来 displayInfoCharFormDetail 的成员函数。这意味着您必须使用 ChartFormDetail 实例来调用它。

要使其工作,您需要执行以下操作:

ChartFormDetail & details = getDetails();
details.displayInfo();

这只是一个示例。我不知道如何获取 ChartFormDetails 的实例,这很大程度上取决于您的架构。

It looks like displayInfo is a member-function of CharFormDetail. That means you have to call it with an instance of ChartFormDetail.

To make it work, you need to do something like this:

ChartFormDetail & details = getDetails();
details.displayInfo();

This is just an example. I don't know how you would get an instance of ChartFormDetails, this depends very much on your architecture.

静水深流 2024-12-28 08:47:48

您如何尝试在班级中调用 displayInfo() ?您需要一个“detailedForm”对象才能访问它。另外,如果您需要访问友元类 (detailedForm) 中的 listView 数据,您将需要对 listView 对象的引用。

如果您正在寻找示例来了解如何使用友元函数,您可以查看:

How are you trying to call displayInfo() in your class? You need an object of 'detailedForm' to access it. Also, if you need access the listView data in your friend class (detailedForm), you will need a reference to listView object.

If you're looking for an example to see how friend functions are used, you could check out: http://www.learncpp.com/cpp-tutorial/813-friend-functions-and-classes/

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