Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C N课程链接列表未显示在屏幕中_C_Linked List_Ncurses_Curses - Fatal编程技术网

C N课程链接列表未显示在屏幕中

C N课程链接列表未显示在屏幕中,c,linked-list,ncurses,curses,C,Linked List,Ncurses,Curses,我制作了一个链表,并用4个“#”字符填充,我想用ncurses库在屏幕上打印它们,因为我想制作一个蛇游戏,但idk为什么不打印,下面是代码: #包括 #包括 结构蛇{ 炭片; struct snake*nextPiece; }; 结构snake*addNewPiece(常量字符片段){ struct snake*newPiece=NULL; newPiece=malloc(sizeof(struct snake)); 如果(新件!=NULL){ newPiece->nextPiece=NULL;

我制作了一个链表,并用4个“#”字符填充,我想用ncurses库在屏幕上打印它们,因为我想制作一个蛇游戏,但idk为什么不打印,下面是代码:

#包括
#包括
结构蛇{
炭片;
struct snake*nextPiece;
};
结构snake*addNewPiece(常量字符片段){
struct snake*newPiece=NULL;
newPiece=malloc(sizeof(struct snake));
如果(新件!=NULL){
newPiece->nextPiece=NULL;
新件->件=件;
}否则{
endwin();
printf(“内存分配失败,程序无法正确分配内存”);
}
返回新片;
}
int main(){
int i=0;
struct snake*first=NULL;
struct snake*added=NULL;
initscr();
noecho();
int yMaxStd、xMaxStd;
getmaxyx(stdscr、yMaxStd、xMaxStd);
WINDOW*win=newwin(20,50,(yMaxStd/2)-10,10);
盒子(赢,0,0);
刷新();
雷弗斯(赢);
leaveok(赢,真);
int yMaxWin、xMaxWin;
getmaxyx(win、yMaxWin、xMaxWin);
wmove(win、yMaxWin/2、yMaxWin/2);
//利纳利斯塔监狱4号#
while(exectpiece=addNewPiece(“#”);
如果(添加->下一页!=NULL){
添加=添加->下一页;
}
}
}
if(first==NULL){
endwin();
printf(“男性alloc失败”);
}
打印屏幕(第一,赢);
getch();
endwin();
返回0;
}
无效打印屏幕(结构蛇*块,窗口*赢){
结构snake*t;
t=件;
int y=5,x=5;
如果(t==NULL){
endwin();
}否则{
while(t!=NULL){
mvwprintw(赢,y,x,t);
t=t->nextPiece;
y--;
}
}
}

<>我知道我很好的链接链表的过程,因为我测试了它在另一个文件中用STDIO .h打印,它工作

你的程序有几个错误,我已经修复了它们。最严重的是你的代码不能用C++编译。如果它是在C++下编译的,你首先需要修复编译错误。/p> < > >代码> NeXTea= MalOC(SigeOf(StultSnake));,这里需要转换:<代码> NeaTe饰件=(Snake)Maloc(SigeOf(StultSnake));,一般不使用C++中的“代码> MalOC >,更自然地使用<代码>新< /COD>

  • first=addNewPiece(“#”);
    将字符串文本作为
    char
    参数传递,需要在此处传递字符

  • mvwprintw(win,y,x,t);
    ,您误解了API,应该将其修复为
    mvwprintw(win,y,x,“%c”,t->piece);
    ,在使用库API之前需要查看

  • 打印屏幕后忘记刷新屏幕

  • 您没有在for循环中增加索引,它是一个无限循环

  • <>你的代码有点C风格,如果你想用C++写,需要重构,有一些建议:

    • 使用
      std::vector
      来存储蛇身,这样我们就不需要手动管理内存。并且避免了容易出错的链表处理。这样就可以简化大部分代码
    • 使用日志库和打印日志来帮助调试
    固定的代码应该可以工作,我的控制台上有一条垂直的蛇

    #包括
    #包括
    结构蛇{
    炭片;
    struct snake*nextPiece;
    };
    结构snake*addNewPiece(常量字符片段){
    struct snake*newPiece=NULL;
    newPiece=(snake*)malloc(sizeof(struct snake));
    如果(新件!=NULL){
    newPiece->nextPiece=NULL;
    新件->件=件;
    }否则{
    endwin();
    printf(“内存分配失败,程序无法正确分配内存”);
    }
    返回新片;
    }
    无效打印屏幕(结构蛇*块,窗口*赢);
    int main(int argc,char*argv[]){
    int i=0;
    struct snake*first=NULL;
    struct snake*added=NULL;
    initscr();
    noecho();
    int yMaxStd、xMaxStd;
    getmaxyx(stdscr、yMaxStd、xMaxStd);
    WINDOW*win=newwin(20,50,(yMaxStd/2)-10,10);
    盒子(赢,0,0);
    刷新();
    雷弗斯(赢);
    leaveok(赢,真);
    int yMaxWin、xMaxWin;
    getmaxyx(win、yMaxWin、xMaxWin);
    wmove(win、yMaxWin/2、yMaxWin/2);
    //利纳利斯塔监狱4号#
    而(i nextPiece=addNewPiece(“#”);
    如果(添加->下一页!=NULL){
    添加=添加->下一页;
    }
    }
    ++一,;
    }
    if(first==NULL){
    endwin();
    printf(“男性alloc失败”);
    }
    打印屏幕(第一,赢);
    刷新();
    雷弗斯(赢);
    getch();
    endwin();
    返回0;
    }
    无效打印屏幕(结构蛇*piece,窗口*win){
    结构snake*t;
    t=件;
    int y=5,x=5;
    如果(t==NULL){
    endwin();
    }否则{
    while(t!=NULL){
    mvwprintw(赢,y,x,“%c”,t->件);
    t=t->nextPiece;
    y--;
    }
    }
    }
    
    在看到PO的评论后,我们知道问题被错误地标记为
    c++
    ,c版本代码:

    #包括
    #包括
    结构蛇{
    炭片;
    struct snake*nextPiece;
    };
    结构snake*addNewPiece(常量字符片段){
    struct snake*newPiece=NULL;
    newPiece=malloc(sizeof(struct snake));
    如果(新件!=NULL){
    newPiece->nextPiece=NULL;
    新件->件=件;
    }否则{
    endwin();
    printf(“内存分配失败,程序无法正确分配内存”);
    }
    返回新片;
    }
    无效打印屏幕(结构蛇*块,窗口*赢);
    int main(int argc,char*argv[]){
    int i=0;
    struct snake*first=NULL;
    struct snake*added=NULL;
    initscr();
    noecho();
    int yMaxStd、xMaxStd;
    getmaxyx(stdscr、yMaxStd、xMaxStd);
    WINDOW*win=newwin(20,50,(yMaxStd/2)-10,10);
    盒子(赢,0,0);
    刷新();
    雷弗斯(赢);
    leaveok(赢,真);
    int yMaxWin、xMaxWin;
    getmaxyx(win、yMaxWin、xMaxWin);
    wmove(win、yMaxWin/2、yMaxWin/2);
    //利纳利斯塔监狱4号#
    while(i nextPiece=addNewPiece(“#”)