Visual c++ 函数调用Visual c+;时未找到标识符错误+;

Visual c++ 函数调用Visual c+;时未找到标识符错误+;,visual-c++,Visual C++,我正在创建一个迷你控制台游戏,突然它无法编译,因为在所有函数中都出现了标识符未找到错误(C3861)。我认为这不是代码问题,因为当我有两个发烧函数时,它可以工作。现在所有函数都不起作用了(我已经尝试将所有函数移到出现问题的Virables部分之前)。下面是代码的重要部分。 编辑:问题仅限于类(struct)中的函数 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; //-------------------------------------------美德---------

我正在创建一个迷你控制台游戏,突然它无法编译,因为在所有函数中都出现了标识符未找到错误(C3861)。我认为这不是代码问题,因为当我有两个发烧函数时,它可以工作。现在所有函数都不起作用了(我已经尝试将所有函数移到出现问题的Virables部分之前)。下面是代码的重要部分。 编辑:问题仅限于类(struct)中的函数

#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//-------------------------------------------美德-------------------------------------------//
结构级别对象
{
字符串名;
线状外观;
bool enabled=true;
bool可交互=false;
void交互(字符串请求、字符串文本、字符串项给定)
{
if(可交互==true)
{
如果(要求==“无”)
{

cout问题在于,在定义或声明某些函数之前(在
LevelObject::Interact
GiveItem
RemovetItem
),您正在使用(调用)它们。但是,在定义/声明
Player
结构之前,您无法定义这些函数

为了解决这个问题,您可以提供-这些是函数的“空”声明,只需指定它们的“形式”(或签名)。将它们放在代码顶部附近(对所有函数都这样做是一种很好的做法,即使这些预声明不是严格必需的)。下面是一种典型的方法:

/---------------------------------------原型------------------------------------------//
void removietem(字符串ItemName2);
bool ItemCheck(字符串ItemName1);
void TakeItem(字符串ItemName0);
void GiveItem(字符串ItemName);
void ColorText(std::string Text0,int Color,bool endl);
作废存货();
//-------------------------------------------美德-------------------------------------------//
ItemCheck
函数中也存在问题,其中for
循环中的
bool T_F=true;
行声明了一个新的变量,
T_F
,该变量“隐藏”(并替换)在函数级作用域中声明的同名变量。从此行中删除
bool
,以便设置该函数级变量的值:

bool ItemCheck(字符串ItemName1)
{
bool T_F=假;
对于(int i=0;i<20;){
if(player.inv[i].name==ItemName1){
T_F=true;//“bool=T_F=true”隐藏(替换)另一个T_F变量!
打破
}
i++;
}
返回T_F;
}

另外,要获得良好的编码指导,请阅读以下内容:。

问题是,在定义或声明某些函数之前,您正在使用(调用)它们(
GiveItem
removeTime
,在
LevelObject::Interact
)。但是,在定义/声明
Player
结构之前,不能定义这些函数

为了解决这个问题,您可以提供-这些是函数的“空”声明,只需指定它们的“形式”(或签名)。将它们放在代码顶部附近(对所有函数都这样做是一种很好的做法,即使这些预声明不是严格必需的)。下面是一种典型的方法:

/---------------------------------------原型------------------------------------------//
void removietem(字符串ItemName2);
bool ItemCheck(字符串ItemName1);
void TakeItem(字符串ItemName0);
void GiveItem(字符串ItemName);
void ColorText(std::string Text0,int Color,bool endl);
作废存货();
//-------------------------------------------美德-------------------------------------------//
ItemCheck
函数中也存在问题,其中for循环中的
bool T_F=true;
行声明了一个新的变量,
T_F
,该变量“隐藏”(并替换)在函数级作用域中声明的同名变量。从此行中删除
bool
,以便设置该函数级变量的值:

bool ItemCheck(字符串ItemName1)
{
bool T_F=假;
对于(int i=0;i<20;){
if(player.inv[i].name==ItemName1){
T_F=true;//“bool=T_F=true”隐藏(替换)另一个T_F变量!
打破
}
i++;
}
返回T_F;
}
此外,有关良好的编码准则,请阅读以下内容:

#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;
    }
}