1

I'm creating a mini console game and suddenly it couldn't compile because of identifier not found error(C3861) in all functions. I think that's not a code problem becouse when i had 2 fever functions it worked. Now none of the functions work (i've tried moving all functions before the Virables section where the problem appears). Here is the important part of the code. Edit: The problem is only to functions that are in class'es (struct)

#include <iostream>
#include <string>
#include <Windows.h>
#include <cstdlib>
#include <time.h>
#include <random>

using namespace std;


//-------------------------------------------Virables-------------------------------------------//
struct LevelObject
{
    string name;
    string look;
    bool enabled = true;
    bool interactable = false;

    void Interact(string requaierment,string Text,string itemGiven)
    {
        if (interactable == true)
        {
            if (requaierment == "none")
            {
                cout << Text << endl;
                GiveItem(itemGiven);
            }
            else if (ItemCheck(requaierment))
            {
                cout << Text << endl;
                GiveItem(itemGiven);
                RemoveItem(requaierment);
            }
            else
            {
                cout << "You don't have the needed item to do this" << endl;
            }
        }
        else
        {
            cout << "It seems like you can't do anything with this" << endl;
        }
    }
};

struct Item
{
    string name;
    bool isHere = false;
    string description;
};

struct Level
{
    int id = 0;
    string LvlText, Look;
    Item item[5];
    LevelObject Obj[3];
    int foward_level = 100;
    int back_level = 100;
    int left_level = 100;
    int right_level = 100;
};
struct Player
{
    Level CurrentLevel;

    Item inv[20];
};

Player player;
Level level[50];
string Input;
//-------------------------------------------Functions------------------------------------------//

void RemoveItem(string ItemName2)
{
    for (int i = 0; i < 20;)
    {
        if (player.inv[i].name == ItemName2)
        {
            player.inv[i].name = "";
            break;
        }

        i++;
    }
}

bool ItemCheck(string ItemName1)
{
    bool T_F = false;
    for (int i = 0; i < 20;)
    {
        if (player.inv[i].name == ItemName1)
        {
            bool T_F = true;
            break;
        }

        i++;
    }
    return T_F;
}

void TakeItem(string ItemName0)
{
    for (int i = 0; i < 3;)
    {
        if (player.CurrentLevel.item[i].name == ItemName0 && player.CurrentLevel.item[i].isHere == true)
        {
            GiveItem(ItemName0);
            player.CurrentLevel.item[i].isHere = false;
            break;
        }
        if (i == 2)
        {
            cout << "There is no such item" << endl;
        }
        i++;
    }
}

void GiveItem(string ItemName)
{
    for (int i = 0; i < 20;)
    {
        if (player.inv[i].name == "")
        {
            player.inv[i].name = ItemName;
            break;
        }

        i++;
    }
}


void ColorText(std::string Text0, int Color, bool endl)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Color);
    if (endl == true)
    {
        std::cout << Text0 << std::endl;
    }
    else if (endl == false)
    {
        std::cout << Text0;
    }
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}



void inventory()
{
    ColorText("---------INVENTORY--------", 9, true);
    for (int i = 0; i < 20;)
    {
        if (player.inv[i].name != "")
            cout << "- " << player.inv[i].name << endl;
        i += 1;
    }
    ColorText("--------------------------", 9, true);
}

int main()
{
    while (true)
    {
        cin >> Input;
    }
}
Laiter
  • 9
  • 3

1 Answers1

0

The problem is that you are using (calling) some of your functions before you define or declare them (GiveItem and RemovetItem in LevelObject::Interact). However, you can't define those functions before you have defined/declared the Player structure!

To get round this problem, you can provide function prototypes - these are 'empty' declarations of the functions, which simply specify their 'form' (or signatures). Place these near the top of your code (it's good practice to do so for all functions, even if these pre-declaraions aren't strictly necessary). Here's a typical way to do this:

// -----------------------------------------Prototypes------------------------------------------//

void RemoveItem(string ItemName2);
bool ItemCheck(string ItemName1);
void TakeItem(string ItemName0);
void GiveItem(string ItemName);
void ColorText(std::string Text0, int Color, bool endl);
void inventory();

//-------------------------------------------Virables-------------------------------------------//

You also have a problem in your ItemCheck function, where your line bool T_F = true; inside the for loop declares a new variable, T_F that 'hides' (and replaces) the variable with the same name declared at function-level scope. Remove the bool from this line, so that you are setting that function-level variable's value:

bool ItemCheck(string ItemName1)
{
    bool T_F = false;
    for (int i = 0; i < 20;) {
        if (player.inv[i].name == ItemName1) {
            T_F = true;// "bool = T_F = true"; hides (replaces) the other T_F variable!
            break;
        }
        i++;
    }
    return T_F;
}

Also, for good coding guidelines, please read this: Why is "using namespace std;" considered bad practice?.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83