首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我在这里得到“`parseAndExecute”错误的“未定义的引用”?

为什么我在这里得到“`parseAndExecute”错误的“未定义的引用”?
EN

Stack Overflow用户
提问于 2022-10-14 01:19:20
回答 2查看 47关注 0票数 -1

我正在使用一个在线教程如何制作一个文本冒险游戏(在VSC),并遇到了一个非常恼人的问题。即使在复制了教程中的所有源代码之后,我也会收到一个“未定义的引用”错误。我试着编辑“tasks.json”文件,如下所述:undefined reference error in VScode,但没有成功。

游戏由多个c文件组成,为了工作,源代码位于页面底部:https://helderman.github.io/htpataic/htpataic05.html

这是所有的文件,谢谢您的帮助!

代码语言:javascript
运行
复制
    <main.c>

#include <stdbool.h>
#include <stdio.h>
#include "parsexec.h"


static char input[100] = "look around";

static bool getInput(void)
{
   printf("\n--> ");
   return fgets(input, sizeof input, stdin) != NULL;
}

int main()
{
   printf("Welcome to Little Cave Adventure.\n");
   while (parseAndExecute(input) && getInput());
   printf("\nBye!\n");
   return 0;
}

代码语言:javascript
运行
复制
<parsexec.h>

   

 extern bool parseAndExecute(char *input);

代码语言:javascript
运行
复制
    <parsexec.c>

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "location.h"
#include "inventory.h"

bool parseAndExecute(char *input)
{
   char *verb = strtok(input, " \n");
   char *noun = strtok(NULL, " \n");
   if (verb != NULL)
   {
      if (strcmp(verb, "quit") == 0)
      {
         return false;
      }
      else if (strcmp(verb, "look") == 0)
      {
         executeLook(noun);
      }
      else if (strcmp(verb, "go") == 0)
      {
         executeGo(noun);
      }
      else if (strcmp(verb, "get") == 0)
      {
         executeGet(noun);
      }
      else if (strcmp(verb, "drop") == 0)
      {
         executeDrop(noun);
      }
      else if (strcmp(verb, "give") == 0)
      {
         executeGive(noun);
      }
      else if (strcmp(verb, "ask") == 0)
      {
         executeAsk(noun);
      }
      else if (strcmp(verb, "inventory") == 0)
      {
         executeInventory();
      }
      else
      {
         printf("I don't know how to '%s'.\n", verb);
      }
   }
   return true;
}

代码语言:javascript
运行
复制
<location.c>

#include <stdio.h>
#include <string.h>
#include "object.h"
#include "misc.h"
#include "noun.h"

void executeLook(const char *noun)
{
   if (noun != NULL && strcmp(noun, "around") == 0)
   {
      printf("You are in %s.\n", player->location->description);
      listObjectsAtLocation(player->location);
   }
   else
   {
      printf("I don't understand what you want to see.\n");
   }
}

void executeGo(const char *noun)
{
   OBJECT *obj = getVisible("where you want to go", noun);
   if (obj == NULL)
   {
      // already handled by getVisible
   }
   else if (obj->location == NULL && obj != player->location)
   {
      printf("OK.\n");
      player->location = obj;
      executeLook("around");
   }
   else
   {
      printf("You can't get much closer than this.\n");
   }
}

代码语言:javascript
运行
复制
  <location.h>
    extern void executeLook(const char *noun);
    extern void executeGo(const char *noun);

代码语言:javascript
运行
复制
<object.c>
#include <stdio.h>
#include "object.h"

OBJECT objs[] = {
   {"an open field", "field"   , NULL  },
   {"a little cave", "cave"    , NULL  },
   {"a silver coin", "silver"  , field },
   {"a gold coin"  , "gold"    , cave  },
   {"a burly guard", "guard"   , field },
   {"yourself"     , "yourself", field }
};

代码语言:javascript
运行
复制
<object.h>
typedef struct object {
   const char    *description;
   const char    *tag;
   struct object *location;
} OBJECT;

extern OBJECT objs[];

#define field      (objs + 0)
#define cave       (objs + 1)
#define silver     (objs + 2)
#define gold       (objs + 3)
#define guard      (objs + 4)
#define player     (objs + 5)

#define endOfObjs  (objs + 6)

代码语言:javascript
运行
复制
<noun.c>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "object.h"

static bool objectHasTag(OBJECT *obj, const char *noun)
{
   return noun != NULL && *noun != '\0' && strcmp(noun, obj->tag) == 0;
}

static OBJECT *getObject(const char *noun)
{
   OBJECT *obj, *res = NULL;
   for (obj = objs; obj < endOfObjs; obj++)
   {
      if (objectHasTag(obj, noun))
      {
         res = obj;
      }
   }
   return res;
}

OBJECT *getVisible(const char *intention, const char *noun)
{
   OBJECT *obj = getObject(noun);
   if (obj == NULL)
   {
      printf("I don't understand %s.\n", intention);
   }
   else if (!(obj == player ||
              obj == player->location ||
              obj->location == player ||
              obj->location == player->location ||
              obj->location == NULL ||
              obj->location->location == player ||
              obj->location->location == player->location))
   {
      printf("You don't see any %s here.\n", noun);
      obj = NULL;
   }
   return obj;
}

OBJECT *getPossession(OBJECT *from, const char *verb, const char *noun)
{
   OBJECT *obj = NULL;
   if (from == NULL)
   {
      printf("I don't understand who you want to %s.\n", verb);
   }
   else if ((obj = getObject(noun)) == NULL)
   {
      printf("I don't understand what you want to %s.\n", verb);
   }
   else if (obj == from)
   {
      printf("You should not be doing that to %s.\n", obj->description);
      obj = NULL;
   }
   else if (obj->location != from)
   {
      if (from == player)
      {
         printf("You are not holding any %s.\n", noun);
      }
      else
      {
         printf("There appears to be no %s you can get from %s.\n",
                noun, from->description);
      }
      obj = NULL;
   }
   return obj;
}

代码语言:javascript
运行
复制
<noun.h>
extern OBJECT *getVisible(const char *intention, const char *noun);
extern OBJECT *getPossession(OBJECT *from, const char *verb, const char *noun);

代码语言:javascript
运行
复制
<inventory.c>
#include <stdio.h>
#include "object.h"
#include "misc.h"
#include "noun.h"
#include "move.h"

void executeGet(const char *noun)
{
   OBJECT *obj = getVisible("what you want to get", noun);
   if (obj == NULL)
   {
      // already handled by getVisible
   }
   else if (obj == player)
   {
      printf("You should not be doing that to yourself.\n");
   }
   else if (obj->location == player)
   {
      printf("You already have %s.\n", obj->description);
   }
   else if (obj->location == guard)
   {
      printf("You should ask %s nicely.\n", obj->location->description);
   }
   else
   {
      moveObject(obj, player);
   }
}

void executeDrop(const char *noun)
{
   moveObject(getPossession(player, "drop", noun), player->location);
}

void executeAsk(const char *noun)
{
   moveObject(getPossession(actorHere(), "ask", noun), player);
}

void executeGive(const char *noun)
{
   moveObject(getPossession(player, "give", noun), actorHere());
}

void executeInventory(void)
{
   if (listObjectsAtLocation(player) == 0)
   {
      printf("You are empty-handed.\n");
   }
}

代码语言:javascript
运行
复制
<inventory.h>
extern void executeGet(const char *noun);
extern void executeDrop(const char *noun);
extern void executeAsk(const char *noun);
extern void executeGive(const char *noun);
extern void executeInventory(void);

代码语言:javascript
运行
复制
<misc.c>
#include <stdio.h>
#include "object.h"

OBJECT *actorHere(void)
{
   OBJECT *obj;
   for (obj = objs; obj < endOfObjs; obj++)
   {
      if (obj->location == player->location && obj == guard)
      {
         return obj;
      }
   }
   return NULL;
}

int listObjectsAtLocation(OBJECT *location)
{
   int count = 0;
   OBJECT *obj;
   for (obj = objs; obj < endOfObjs; obj++)
   {
      if (obj != player && obj->location == location)
      {
         if (count++ == 0)
         {
            printf("You see:\n");
         }
         printf("%s\n", obj->description);
      }
   }
   return count;
}

代码语言:javascript
运行
复制
<misc.h>

    extern OBJECT *actorHere(void);
    extern int listObjectsAtLocation(OBJECT *location);

代码语言:javascript
运行
复制
<move.c>
#include <stdio.h>
#include "object.h"

static void describeMove(OBJECT *obj, OBJECT *to)
{
   if (to == player->location)
   {
      printf("You drop %s.\n", obj->description);
   }
   else if (to != player)
   {
      printf(to == guard ? "You give %s to %s.\n" : "You put %s in %s.\n",
             obj->description, to->description);
   }
   else if (obj->location == player->location)
   {
      printf("You pick up %s.\n", obj->description);
   }
   else
   {
      printf("You get %s from %s.\n",
             obj->description, obj->location->description);
   }
}

void moveObject(OBJECT *obj, OBJECT *to)
{
   if (obj == NULL)
   {
      // already handled by getVisible or getPossession
   }
   else if (to == NULL)
   {
      printf("There is nobody here to give that to.\n");
   }
   else if (obj->location == NULL)
   {
      printf("That is way too heavy.\n");
   }
   else
   {
      describeMove(obj, to);
      obj->location = to;
   }
}

代码语言:javascript
运行
复制
<move.h>
extern void moveObject(OBJECT *obj, OBJECT *to);

VSC:https://i.stack.imgur.com/5mkzV.png中问题的图像

EN

Stack Overflow用户

发布于 2022-10-14 18:43:16

由于代码在另一台机器上运行良好,因此在VSC中出现了错误的编译器/设置。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74063362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档