两个未初始化的实例之一
One of two instances not getting initialized
我正在使用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
实例...
相关文章:
- C++初始化类实例时隐式调用类成员的构造函数
- 初始化指向类实例的智能指针并访问其方法
- 我可以初始化 const 实例,以便我可以将其用作 const 来初始化数组吗?
- c++ 是否保证标头初始化的静态 const 成员跨编译单元和库共享单个实例?
- 具有聚合初始化的C++常量匿名实例
- oFx:使用 mouseEvent 初始化类实例
- 我可以从实例列表中初始化 std::vector<T*> 吗?
- 在声明或实现中延迟初始化C++单一实例
- 是否可以使用将不同参数传递给构造函数的类的不同实例初始化向量
- 在C++私有区域中初始化实例变量
- 可以使用C 结构汇总初始化来创建临时实例
- 使用类/对象实例数据初始化对话框
- 实例的全局初始化
- 从模板类初始化对象时出错.参考类模板实例化
- C++ - 使用实例初始化类成员
- 编译时的单一实例初始化
- c++变量/实例初始化跨不同翻译单元的顺序
- 用临时实例初始化对象
- 只在类的第一个实例初始化时调用的成员函数(c++)
- 使用另一个类的多个实例初始化