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

时间:2019-04-09 16:05:13

标签: c

我已经编写了一个程序来创建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$ 

3 个答案:

答案 0 :(得分:0)

您在哪里:

int main();

  const char *animalArray[] = {

您应该拥有:

int main()
{
  const char *animalArray[] = {

答案 1 :(得分:0)

您似乎没有main函数。我看到那条线

int main();

,但未在任何地方实现。您得到的错误是因为编译器不知道从哪里启动程序。也许这是一个错字。

也许您不是要关闭fillWordsearch函数并启动main()函数。

}

int main()
{

答案 2 :(得分:0)

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

int main();

这是函数声明,而不是定义。看来您已经忘记了main()函数主体周围的花括号。

我还看到您一直在所有功能的右括号后面加上分号。这不是必需的,这让我想知道您正在使用什么可怕的资源来学习C。

编辑:

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