gpt4 book ai didi

C- 对 <函数名称> 的 undefined reference

转载 作者:行者123 更新时间:2023-11-30 18:15:02 25 4
gpt4 key购买 nike

我是 C 编程新手,我花了几个小时尝试修复“ undefined reference ”错误。

我需要编写战舰游戏代码,现在我只想在屏幕上打印游戏 map ,我的代码可能对你没有意义,但我才刚刚开始并试图修复此错误。

我的代码

#include <stdio.h>
/*functions declaration*/
void mapmaker(char map[8][8]);
void printmap(char map[8][8]);

int main()
{
char map[8][8];

mapmaker(map);
printmap(map);

}



void mapmaker(char map[8][8])
{
int i,z;
/*creating map*/
for(i=0;i<8;i++){
for(z=0;z<8;z++)
{
map[i][z]='~';
}

}



void printmap(char map[8][8])
{
int num[8]={0,1,2,3,4,5,6,7};
int i,z;

for(i=0;i<8;i++)
printf("%d",num[i]);

printf("\n--------------------------------\n");

for(i=0;i<8;i++)
printf("%d|",num[i]);
for(z=0;z<8;z++)
printf("%c ",map[i][z]);
printf("\n");
}

}

当它尝试运行该函数时发生错误 ->“printmap(map)”然后我收到“ undefined reference “printmap(map)””错误。

非常感谢!

最佳答案

您没有正确关闭函数 printmap 的括号,在最后添加了一个额外的括号。并且在mapmaker中您也没有放置mapmaker函数的结束括号。这将解决您的问题。

#include <stdio.h>
/*functions declaration*/
void mapmaker(char map[8][8]);
void printmap(char map[8][8]);

int main()
{
char map[8][8];

mapmaker(map);
printmap(map);
return 0;
}
void mapmaker(char map[8][8])
{
int i,z;
/*creating map*/
for(i=0;i<8;i++){
for(z=0;z<8;z++)
{
map[i][z]='~';
}
}
} //this was missing
void printmap(char map[8][8])
{
int num[8]={0,1,2,3,4,5,6,7};
int i,z;

for(i=0;i<8;i++)
printf("%d",num[i]);

printf("\n--------------------------------\n");

for(i=0;i<8;i++)
printf("%d|",num[i]);
for(z=0;z<8;z++)
printf("%c ",map[i][z]); //in for loop
printf("\n"); //not in for loop
}

// } //this was extra

关于C- 对 <函数名称> 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43004049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com