LNK2019错误;这里缺少/未定义什么?

时间:2019-04-03 03:57:17

标签: c++ build linker-errors lnk2019

当我尝试构建我的程序时,出现此错误:

error LNK2019: unresolved external symbol "bool __cdecl  TextGame::spaceVoid(struct TextGame::Position const &,struct TextGame::RoomData const &)" (?spaceVoid@TextGame@@YA_NABUPosition@1@ABURoomData@1@@Z) referenced in function "void __cdecl TextGame::UpdateGame(struct TextGame::PlayerState &,struct TextGame::WorldState &)" (?UpdateGame@TextGame@@YAXAAUPlayerState@1@AAUWorldState@1@@Z)

这是针对基本ASCII地牢搜寻器的。我是上大学的相对新手。现在,我们只是跟着一对一视频一起观看,所以我不完全确定错误在哪里。显然,似乎需要定义一些有关Position(房间中对象的坐标)和RoomData(房间的属性)的东西。但是我迷失了到底是什么。抱歉,我无法提供具体信息-如果可以提供其他信息,请告诉我。

namespace TextGame
{
RoomData CreateRoom(const std::string& inName, const std::string& inDescription)
{
    RoomData room = {};
    room.Name = inName;
    room.Description = inDescription;

    return room;
}

void InitializeGame(PlayerState& playerState, WorldState& worldState)
{
    printf("Welcome to...\n");
    printf("A GAME by SOMEONE\n\n");
    printf("TELL THE PLAYER THE GOAL OF THE GAME\n\n");

    playerState.WantsDescription = true;
    playerState.CurrentRoomIndex = 0;
    playerState.CurrentPosition.X = 3;
    playerState.CurrentPosition.Y = 4;

    worldState.Rooms.clear();

    // 0
    {
        RoomData room = CreateRoom("ROOM NAME", "ROOM DESCRIPTION");
        room.RoomMapWidth = 10;
        room.RoomPosition = { 0, 0 };
        room.RoomMap =
            "####.#####"
            "#........#"
            "#........#"
            "#........."
            "#........."
            "#........#"
            "#........#"
            "##########";

        room.Inventory.push_back({ ItemType_Key, {5,3} });
        room.Inventory.push_back({ ItemType_Sword, {7,5} });
        room.LockedDoors.push_back({ {4, 0} });

        worldState.Rooms.push_back(room);
    }

    // 1
    {
        RoomData room = CreateRoom("ROOM NAME 1", "ROOM DESCRIPTION 1");
        room.RoomMapWidth = 10;
        room.RoomPosition = { 1, 0 };
        room.RoomMap =
            "##########"
            "#........#"
            "#........#"
            ".........#"
            ".........#"
            "#........#"
            "#........#"
            "##########";

        worldState.Rooms.push_back(room);
    }

    // 2
    {
        RoomData room = CreateRoom("ROOM NAME 2", "ROOM DESCRIPTION 2");
        room.RoomMapWidth = 10;
        room.RoomPosition = { 0, -1 };
        room.RoomMap =
            "####.#####"
            "#........#"
            "#........#"
            "#........#"
            "#........#"
            "#........#"
            "#........#"
            "####..####";

        worldState.Rooms.push_back(room);
    }

    // 3
    {
        RoomData room = CreateRoom("ROOM NAME 3", "ROOM DESCRIPTION 3");
        room.RoomMapWidth = 10;
        room.RoomPosition = { 0, -2 };
        room.RoomMap =
            "####..####"
            "#........#"
            "#........#"
            "#........#"
            "#........#"
            "#........#"
            "#........#"
            "####..####";

        worldState.Rooms.push_back(room);
    }

    // 4
    {
        RoomData room = CreateRoom("ROOM NAME 4", "ROOM DESCRIPTION 4");
        room.RoomMapWidth = 10;
        room.RoomPosition = { 0, -3 };
        room.RoomMap =
            "##########"
            "#....A...#"
            "#........#"
            "#........#"
            "#........#"
            "#........#"
            "#........#"
            "####..####";

        worldState.Rooms.push_back(room);
    }
}

void GetInput(PlayerState& playerState, const WorldState& worldState)
{
    playerState.WantsToExit = false;
    playerState.WantsDescription = false;
    playerState.WantsInventoryListed = false;
    playerState.DesiredPosition = playerState.CurrentPosition;
    playerState.WantsToGet = false;

    //playerState.Inventory.push_back({ ItemType_Key, Position() });
    //playerState.Inventory.push_back({ ItemType_Sword, Position() });
    //playerState.Inventory.push_back({ ItemType_Key, Position() });

    printf("What do you do?\n");
    printf("> ");
    TextAdventureCommand command = ParseAdventureCommand();

    if (command.Verb == "quit")
    {
        playerState.WantsToExit = true;
    }
    else if (command.Verb == "north" || command.Verb == "w")
    {
        playerState.DesiredPosition.Y = playerState.DesiredPosition.Y - 1;
    }
    else if (command.Verb == "south" || command.Verb == "s")
    {
        playerState.DesiredPosition.Y = playerState.DesiredPosition.Y + 1;
    }
    else if (command.Verb == "west" || command.Verb == "a")
    {
        playerState.DesiredPosition.X = playerState.DesiredPosition.X - 1;
    }
    else if (command.Verb == "east" || command.Verb == "d")
    {
        playerState.DesiredPosition.X = playerState.DesiredPosition.X + 1;
    }
    else if (command.Verb == "look")
    {
        playerState.WantsDescription = true;
    }
    else if (command.Verb == "inventory" || command.Verb == "inv")
    {
        playerState.WantsInventoryListed = true;
    }
    else if (command.Verb == "get")
    {
        playerState.WantsToGet = true;
    }
    else if (command.Verb == "help")
    {
        printf("Command List: look, quit, inventory, get, w, a, s, d\n");
        printf("Key:\n");
        printf("  @ - Player\n");
        printf("  A - Altar\n");
        printf("  i - Item\n");
        printf("  . - Floor\n");
        printf("  # - Wall\n");
        printf("  H - Door (Locked)\n");
    }
    else
    {
        printf("I don't understand\n");
    }
    printf("\n");

}

void RenderGame(const PlayerState& playerState, const WorldState& worldState)
{
    if (playerState.WantsDescription)
    {
        const RoomData& currRoom = worldState.Rooms[playerState.CurrentRoomIndex];
        printf("================================================\n");
        printf("LOCATION: %s\n", currRoom.Name.c_str());
        printf("%s\n\n", currRoom.Description.c_str());

        std::string renderedMapString = "";
        unsigned int currentSpace = 0;
        while (currentSpace < currRoom.RoomMap.size())
        {
            char CharacterToDisplay = currRoom.RoomMap[currentSpace];

            for (unsigned int i = 0; i < currRoom.Inventory.size(); ++i)
            {
                const InventoryItem& currItem = currRoom.Inventory[i];
                if (PositionToIndex(currItem.ItemPosition, currRoom.RoomMapWidth) == currentSpace)
                {
                    CharacterToDisplay = getItemIcon(currItem.Type);
                }
            }

            for (unsigned int i = 0; i < currRoom.LockedDoors.size(); ++i)
            {
                const LockedDoorData& currDoor = currRoom.LockedDoors[i];
                if (PositionToIndex(currDoor.DoorPosition, currRoom.RoomMapWidth) == currentSpace)
                {
                    CharacterToDisplay = 'H';
                }
            }

            //Set where the player is drawn based on the input X and Y coordinates
            if (PositionToIndex(playerState.CurrentPosition, currRoom.RoomMapWidth) == currentSpace)
            {
                CharacterToDisplay = '@';
            }

            renderedMapString += CharacterToDisplay;

            if (currentSpace % currRoom.RoomMapWidth == currRoom.RoomMapWidth - 1)
            {
                renderedMapString += "\n";
            }

            currentSpace++;
        }

        printf("%s\n", renderedMapString.c_str());

        printf("\n");
    }
    else if (playerState.WantsInventoryListed)
    {
        printf("================================================\n");
        printf("INVENTORY:\n");

        if (playerState.Inventory.size() == 0)
        {
            printf("You're not carrying anything.\n");
        }

        else
        {
            for (unsigned int i = 0; i < playerState.Inventory.size(); ++i)
            {
                printf("%s\n", GetItemName(playerState.Inventory[i].Type).c_str());
            }
        }


        printf("\n");
    }
}

void UpdateGame(PlayerState& playerState, WorldState& worldState)
{
    RoomData& currRoom = worldState.Rooms[playerState.CurrentRoomIndex];

    if (playerState.DesiredPosition != playerState.CurrentPosition)
    {
        TryToUnlockDoor(playerState.DesiredPosition, currRoom, playerState);

        if (spaceVoid(playerState.DesiredPosition, currRoom))
        {
            Position desiredRoomPosition = currRoom.RoomPosition;
            int playerPositionType = 0;
            if (playerState.DesiredPosition.X < 0)
            {
                desiredRoomPosition.X--;
                playerPositionType = 1;
            }
            else if(playerState.DesiredPosition.X >= currRoom.RoomMapWidth)
            {
                desiredRoomPosition.X++;
                playerPositionType = 2;
            }
            else if (playerState.DesiredPosition.Y < 0)
            {
                desiredRoomPosition.Y--;
                playerPositionType = 3;
            }
            else if (playerState.DesiredPosition.Y >= ((int)currRoom.RoomMap.size() / currRoom.RoomMapWidth))
            {
                desiredRoomPosition.Y++;
                playerPositionType = 4;
            }

            bool foundNewRoom = false;

            for (unsigned int i = 0; i < worldState.Rooms.size(); ++i)
            {
                if (worldState.Rooms[i].RoomPosition == desiredRoomPosition)
                {
                    playerState.CurrentRoomIndex = i;
                    playerState.WantsDescription = true;

                    switch (playerPositionType)
                    {
                    case 1:
                        playerState.CurrentPosition.X = worldState.Rooms[playerState.CurrentRoomIndex].RoomMapWidth - 1;
                        break;
                    case 2:
                        playerState.CurrentPosition.X = 0;
                        break;
                    case 3:
                        playerState.CurrentPosition.Y = (worldState.Rooms[playerState.CurrentRoomIndex].RoomMap.size() / worldState.Rooms[playerState.CurrentRoomIndex].RoomMapWidth) - 1;
                        break;
                    case 4:
                        playerState.CurrentPosition.Y = 0;
                        break;
                    }

                    foundNewRoom = true;
                }
            }

            if (!foundNewRoom)
            {
                printf("It's just the void beyond this point.\n");
            }
        }
        else if (spaceOpen(playerState.DesiredPosition, currRoom))
        {
            playerState.CurrentPosition = playerState.DesiredPosition;
            playerState.WantsDescription = true;
        }
        else
        {
            printf("It's blocked.\n");
        }
    }
    else if (playerState.WantsToGet)
    {
        bool foundItem = false;
        for (unsigned int i = 0; i < currRoom.Inventory.size(); ++i)
        {
            const InventoryItem& currItem = currRoom.Inventory[i];
            if (currItem.ItemPosition == playerState.CurrentPosition)
            {
                printf("Picked up a %s\n\n", GetItemName(currItem.Type).c_str());

                playerState.Inventory.push_back(currItem);

                currRoom.Inventory.erase(currRoom.Inventory.begin() + i);

                foundItem = true;
                break;
            }
        }

        if (!foundItem)
        {
            printf("There's nothing her to pick up.\n\n");
        }
    }
}

void CleanupGame(PlayerState& playerState, WorldState& worldState)
{
    worldState.Rooms.clear();
}

int PositionToIndex(const Position& position, int roomWidth)
{
    return position.Y * roomWidth + position.X;
}

bool spaceOpen(const Position& position, const RoomData& currRoom)
{
    for (unsigned int i = 0; i < currRoom.LockedDoors.size(); ++i)
    {
        const LockedDoorData& currDoor = currRoom.LockedDoors[i];
        if (currDoor.DoorPosition == position)
        {
            return false;
        }
    }

    int spaceIndex = PositionToIndex(position, currRoom.RoomMapWidth);
    return currRoom.RoomMap[spaceIndex] == '.';
}

void TryToUnlockDoor(const Position& position, RoomData& currRoom, PlayerState& playerState)
{
    for (unsigned int i = 0; i < currRoom.LockedDoors.size(); ++i)
    {
        const LockedDoorData& currDoor = currRoom.LockedDoors[i];
        if (currDoor.DoorPosition == position)
        {
            //Door found, now find key

            for (unsigned int j = 0; j < playerState.Inventory.size(); ++j)
            {
                const InventoryItem& currItem = playerState.Inventory[j];
                if (currItem.Type == ItemType_Key)
                {
                    //Key found
                    currRoom.LockedDoors.erase(currRoom.LockedDoors.begin() + i);
                    playerState.Inventory.erase(playerState.Inventory.begin() + j);

                    return;
                }
            }
        }
    }
}

bool spaceVoid(const Position& position, const RoomData& currRoom, PlayerState& playerState)
{
    return position.X < 0 || position.X >= currRoom.RoomMapWidth || position.Y < 0 || position.Y >= ((int)currRoom.RoomMap.size() / currRoom.RoomMapWidth);
}

std::string GetItemName(ItemType itemType)
{
    switch (itemType)
    {
    case ItemType_Key:
        return "Key";
        break;
    case ItemType_Sword:
        return "Sword";
        break;
    }
}
char getItemIcon(ItemType itemType)
{
    switch (itemType)
    {
    case TextGame::ItemType_Key:
        return 'k';
        break;
    case TextGame::ItemType_Sword:
        return 's';
        break;
    }
}

}

1 个答案:

答案 0 :(得分:1)

链接器抱怨找不到使用两个参数的spacevoid方法:

bool __cdecl  TextGame::spaceVoid(struct TextGame::Position const &,struct TextGame::RoomData const &)

因为在您的代码中只有一个spacevoid方法,其中定义了三个参数:

bool spaceVoid(const Position& position, const RoomData& currRoom, PlayerState& playerState)

您需要确定哪一个是正确的,或者添加仅使用两个参数的其他spacevoid方法。