两个未初始化的实例之一

One of two instances not getting initialized

本文关键字:实例 初始化 两个      更新时间:2023-10-16

我正在使用ncurses,我正在尝试创建一个可以打印框的类。我的班级

class CMenu{
public:
    CMenu( int x , int y){
        this -> x = x;
        this -> y = y;
        createMenu();
    }   
    void createMenu(){
        start_color();
        cbreak();
        noecho();
        keypad(stdscr, TRUE);
        my_menu_win = newwin(30, 60, x, y);
        keypad(my_menu_win, TRUE);

    /* Print a border around the main window and print a title */
        box(my_menu_win, 0, 0);
        mvprintw(LINES - 2, 0, "F2 to exit");
        refresh();
        wrefresh(my_menu_win);
        while(true){
            switch(c = wgetch(my_menu_win)){
                case KEY_F(2):
                    endwin();
                    exit(1);    
                case KEY_DOWN:
                    break;
                case KEY_UP:
                    break;
                case 10: 
                    move(20, 0);
                    clrtoeol();
                    wrefresh(my_menu_win);
                    refresh();
                    break;
            }
            wrefresh(my_menu_win);
        }
    }
private:
    int n_choices;
    int i;
    int index;
    int c;
    int x;
    int y;
    WINDOW *my_menu_win;
};

以及我想在其中创建所述类的实例的类

class CUI{
public:
    CUI(){
        min_X = 200;
        min_Y = 50; 
    }
    void printBackground(){
        start_color();
        init_pair(1,COLOR_GREEN,COLOR_BLACK);
        attron(COLOR_PAIR(1));
        attroff(COLOR_PAIR(1));
        attron(COLOR_PAIR(1));
        mvhline( (y/5) - 1, (x/6),  ACS_BOARD , 140);
        mvhline( (y/5)*4, (x/6), ACS_BOARD , 140);
    }
    void initMenu(){    
        initscr();
        printBackground();
        left  = new CMenu(10,35);
        right = new CMenu(10,115);
        refresh();
        endwin();
}
private:
    int min_X;
    int min_Y;
    int x;
    int y;
    CMenu *left;
    CMenu *right;
};

和主要

int main() {
    CUI one;
    one.initMenu();
    return 0;
}

问题是只有一个盒子被打印出来。我cout添加到 CMenu 类的构造函数中,以查看它们是否都被调用。只有一个实例被调用,第二个实例被引入。是什么原因造成的?为什么没有调用这两个实例?

创建第一个菜单(left = new CMenu(10,35); )后,其构造函数称为 createMenu() ...而这个开始了一个永远不会结束的无限循环(while(true)语句)。

因此不会创建第二个菜单。 right = new CMenu(10,115);声明永远不会达成...直到CMenu::createMenu()回来...通过读取您的代码,显然没有任何操作可以退出此循环。因此,您的程序永远不会创建多个CMenu实例...