未初始化的两个实例之一

One of two instances not getting initialized

我正在使用 ncurses,我正在尝试创建一个 class 来打印一个框。 我的 class

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 我想创建上述 class

的实例
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 class 的构造函数中,以查看它们是否都被调用。只有一个实例被调用,第二个实例被忽略。是什么原因造成的?为什么两个实例都没有被调用?

在您创建了第一个菜单 (left = new CMenu(10,35);) 之后,它的构造函数调用了 createMenu()...并且这个菜单启动了一个永远不会结束的无限循环(while(true) 语句)。

因此不会创建第二个菜单。 right = new CMenu(10,115); 语句永远不会到达...直到 CMenu::createMenu() returns... 并且通过阅读您的代码,显然没有任何操作可以退出此循环。所以你的程序永远不会创建超过一个 CMenu 个实例...