gpt4 book ai didi

c - 为什么会发生这种情况? (C 编程错误)

转载 作者:行者123 更新时间:2023-11-30 21:38:29 27 4
gpt4 key购买 nike

我编写了一个程序来创建 2D 单词搜索游戏,该游戏允许用户从一个类别中进行选择,并且该程序将生成包含该类别中的单词的单词搜索。但是,当我编译程序时,我收到一个以前从未见过的错误,并且我不知道如何修复它。

这是我得到的错误 -

 TheRealFawcett:Wordsearch therealfawcett$ gcc -o Wordsearch1 
Wordsearch1.c
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
TheRealFawcett:Wordsearch therealfawcett$

这是我的代码 -

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define WIDTH 16
#define HEIGHT 16
#define NWORDS 6

char wordsearch[HEIGHT][WIDTH];

/* horizontaly */

int canPlaceH (const char * word, int i, int j)
{
if ((strlen(word) + j) > WIDTH)
return 0;

do {
if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != *word))
return 0;
j += 1;
} while (*++word);

return 1;
};

void placeH(const char * word, int i, int j)
{
do {
wordsearch[i][j++] = *word;
} while (*++word);
};

/* verticaly */

int canPlaceV(const char * word, int i, int j)
{
if ((strlen(word) + i) > HEIGHT)
return 0;

do {
if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != *word))
return 0;
i += 1;
} while (*++word);

return 1;
};

void placeV(const char * word, int i, int j)
{
do {
wordsearch[i++][j] = *word;
} while (*++word);
};

/* diagonal up */

int canPlaceDU(const char * word, int i, int j)
{
int ln = strlen(word);

if (((ln + j) > WIDTH) || ((i - ln) < 0))
return 0;

do {
if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != *word))
return 0;
i -= 1;
j += 1;
} while (*++word);

return 1;
};

void placeDU(const char * word, int i, int j)
{
do {
wordsearch[i--][j++] = *word;
} while (*++word);
};

/* diagonal down */

int canPlaceDD(const char * word, int i, int j)
{
int ln = strlen(word);

if (((ln + j) > WIDTH) || ((i + ln) > HEIGHT))
return 0;

do {
if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != *word))
return 0;
i += 1;
j += 1;
} while (*++word);

return 1;
};

void placeDD(const char * word, int i, int j)
{
do {
wordsearch[i++][j++] = *word;
} while (*++word);
};

void fillWordsearch(const char ** a, int sz)
{
/* first step add words from a */
const char * used[NWORDS - 1] = { NULL }; /* to not get two times
the same word */

for (int w = 0; w != NWORDS; ++w) {
/* random choice of a word not yet used */
const char * word;

for (;;) {
word = a[rand() % sz];
int i;

/* check not yet used */
for (i = 0; i != w; ++i)
if (!strcmp(used[i], word))
break;

if (i == w) {
/* not yet used */
used[w] = word;
break;
}
};

/* random placement */

int i, j, d;
typedef int (*canFunc)(const char *, int, int);
typedef void (*placeFunc)(const char *, int, int);
const canFunc canPlace[] = { canPlaceH, canPlaceV, canPlaceDU,
canPlaceDD };
const placeFunc place[] = { placeH, placeV, placeDU, placeDD };

do {
i = rand() % HEIGHT;
j = rand() % WIDTH;
d = rand() % 4;
} while (!(*canPlace[d])(word, i, j));
(*place[d])(word, i, j);



#ifdef DEBUG
for (int i = 0; i != HEIGHT; ++i) {
for (int j = 0; j != WIDTH; ++j)
putchar((wordsearch[i][j] == 0) ? '.' : wordsearch[i][j]);
putchar('\n');
};
putchar('\n');
#endif

/* second step fill not yet set characters with random lowercase
letters */
int q,w;

for (q = 0; q < HEIGHT; q++)
for (w = 0; w != WIDTH; ++w)
if (wordsearch[q][w] == 0)
wordsearch[q][w] = 'a' + rand() % ('z' - 'a' + 1);
};

int main();

const char *animalArray[] = {
"lynx",
"kitten",
"cheetah",
"ape",
"doe",
"reindeer",
"whale",
"baboon",
"skunk",
"dugong",
"elephant",
"anteater",
"chameleon",
"lizaed",
"horse"
};

const char *colourArray[] = {
"red",
"green",
"blue",
"black",
"pink",
"yellow",
"brown",
"orange",
"purple",
"black",
"white",
"cyan",
"maroon",
"magenta",
"grey"
};

const char *videogameArray[] = {
"fortnite",
"fifa",
"hytale",
"soma",
"prey",
"destiny",
"titanfall",
"woldenstein",
"battlefield",
"fallout",
"tekken",
"skyrim",
"dishonored",
"uncharted",
"anthem"
};

const char *sportsArray[] = {
"basketball",
"football",
"cricket",
"wrestling",
"fencing",
"rowing",
"volleyball",
"baseball",
"hockey",
"racing",
"golf",
"bobsleigh",
"curling",
"snowboarding",
"bowling"
};

const char *countryArray[] = {
"england",
"ireland",
"china",
"wales",
"bangladesh",
"maldives",
"slovenia",
"uruguay",
"colombia",
"samoa",
"jamaica",
"malta",
"bulgaria",
"armenia",
"gamnbia"
};

printf("--------------------------------\n");
printf("| Welcome to the WordSearch Game|\n");
printf("--------------------------------\n");
printf("Choose a Category - \n");
printf("Option 1 - Animals\n");
printf("Option 2 - Colours\n");
printf("Option 3 - Video Games\n");
printf("Option 4 - Sports\n");
printf("Option 5 - Countries\n");

int i;

if ((scanf("%d", &i) != 1) || (i < 1) || (i > 5))
return;

srand(time(NULL));

switch (i) {
case 1:
fillWordsearch(animalArray,
sizeof(animalArray)/sizeof(*animalArray));
break;
case 2:
fillWordsearch(colourArray,
sizeof(colourArray)/sizeof(*colourArray));
break;
case 3:
fillWordsearch(videogameArray,
sizeof(videogameArray)/sizeof(*videogameArray));
break;
case 4:
fillWordsearch(sportsArray,
sizeof(sportsArray)/sizeof(*sportsArray));
break;
default:
fillWordsearch(countryArray,
sizeof(countryArray)/sizeof(*countryArray));
break;
};

/* print result */
for (i = 0; i != HEIGHT; ++i) {
for (int j = 0; j != WIDTH; ++j)
putchar(wordsearch[i][j]);
putchar('\n');
};

return;
};

这是我收到的新错误,但是当我更改它们时,我收到新错误。

这是我遇到的错误最少的 -

TheRealFawcett:Wordsearch therealfawcett$ gcc -o Wordsearch1 
Wordsearch1.c
Wordsearch1.c:173:1: error: expected identifier or '('
{
^
1 error generated.
TheRealFawcett:Wordsearch therealfawcett$

最佳答案

错误表明您没有提供 main() 函数。查看您的代码,我看到以下内容:

int main();

这是函数声明而不是定义。您似乎忘记了 main() 函数主体周围的花括号。

我还看到您一直在所有函数的右括号后面添加分号。这不是必需的,这让我想知道您使用什么糟糕的资源来学习 C。

编辑:

您的 fillWordsearch() 函数的花括号不平衡。它在某处缺少 } 大括号。

关于c - 为什么会发生这种情况? (C 编程错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55596967/

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