C++:如何将 FLTK 文本框的值存储在另一个类的变量中

C++: How to store the value of an FLTK textbox in a variable within another class

本文关键字:存储 变量 另一个 文本 FLTK C++      更新时间:2023-10-16

这是我的代码框架 -

class FLTK_win;        //class declaration before full definition so it can be used in the other class
//first of the classes
class functions {
                public:
                        void example_function();
.....
};
void example_function() {
                   std::string input = FLTK_win::Textbox->value();
/*so here I want to make a string that takes the value of whatever the user has 
entered in the text box in the FLTK window class*/
........
}
//second class which is a friend of the first
class FLTK_win : public Fl_Window {
    friend class functions;
    Fl_Input* Textbox;
......//and the rest of the stuff to open the FLTK window when an instance of the class is created
};

从这一行:

std::string input = FLTK_win::Textbox->value();

我收到错误:

"incomplete type 'FLTK_win' used in nested name specifier"

我想知道我的课程顺序是否错误?但是,我不明白这会如何给出此错误。

否则,必须有一种不同的(适当的)方法来调用不同类中 FLTK 文本框的值,而不是"FLTK_win::Textbox->value()"?

我尝试在"函数"类中创建一个名为 testwin 的 FLTK_win 实例,然后编写:

testwin.Textbox->value();

但是我认为这不起作用,因为文本框的值不是变量,因此不能以这种方式调用。我也研究过getters和setters,但对它们的了解还不够,不知道它们是否是答案。

提前感谢您的帮助!

如果要访问文本框,最简单的方法是将定义放在函数之前。

class FLTK_win: public Fl_window
{
...
    Fl_Input* Textbox;
...
};

然后声明类的实例

FLTK_win* awin;

在您的函数中,您现在可以使用它,因为编译器现在知道 awin 是什么以及它的成员函数/变量是什么。

    std::string input = awin->Textbox->value();

最后,在主程序中,您需要创建实例

int main()
{
    awin = new FLTK_win();
    ...
}