最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - I have C3861 (isInvEmpty: Identifier not found) in Inventory.cpp (37), but function is there - Stack Overflow

programmeradmin5浏览0评论

I tried to include in my project .h file, but i dont know how to use it right. I use VS22 and it doesnt give me quick fixes.

Inventory.cpp

#include <iostream>
#include <string>
#include "Inventory.h"

using namespace std;
const unsigned int inventoryCapacity = 10;
int itemsCount;
string inventoryObjects[inventoryCapacity];

namespace BP {

    bool isInvFull()
    {
        if (itemsCount == 10)
        {
            return true;
        }
        else {
            return false;
        }
    }

    void main() {
        try {
            throw exception("Exception");
        }
        catch (...) {
            cout << "Exception!" << endl;
        }
    }

    void openInventory()
    {
        itemsCount = sizeof(inventoryObjects);

        cout << "items in your backpack:\n";
        if (!isInvEmpty())
        {
            for (int i = 0; i < itemsCount; i++)
            {
                cout << inventoryObjects[i] << "\n" << endl;
            }
            cout << "Items: " << itemsCount << "/" << inventoryCapacity << ".";
            if (isInvFull())
            {
                cout << " Your inventory is full.\n" << endl;
            }
            else {
                cout << ".\n" << endl;
            }
        }
        else {
            cout << "Your Inventory is empty...\n" << endl;
        }
    }

    bool isInvEmpty()
    {
        if (itemsCount == 0)
        {
            return true;
        }
        else {
            return false;
        }
    }

    void giveItem(string itemName)
    {
        if (!isInvEmpty()) { //Error there C3861
            int currentItem = sizeof(inventoryObjects);
            inventoryObjects[currentItem + 1] = itemName;
            cout << "Item \'" << itemName << "\' given!\n" << endl;
        }
        else {
            cout << "Inventory is full!\n" << endl;
        }
    }
}

main.cpp

#include <string>
#include <iostream>
#include "Inventory.cpp"

using namespace std;
using namespace BP;
const string COMMAND_LIST[] = {"help", "inventory", "cls", "giveitem", "givemoney", "delitem", "delmoney"};
int commandsCount = sizeof(COMMAND_LIST);
string input;
string itemName;

int main() {
    giveItem("Sword");

    while (true)
    {
        cin >> input;

        if (input == "help") {
            for (int i = 0; i < commandsCount; i++)
            {
                cout << COMMAND_LIST[i] << "\n" << endl;
            }
        }
        else if (input == "inventory") {
            openInventory();
        }
        else if (input == "cls") {
            system("cls");
        }
        else if ("giveitem") {
            cout << "Item name: ";
            cin >> itemName;
            giveItem(itemName);
        }
    }
    return 0;
}

I just do something and now I have only one error (See post title).

Before: 2 with isInvFull 2 with isInvEmpty

Also I have 2 yellow flags:

Reading invalid data from 'inventoryObjects': the readable size is '400' bytes, but '16080' bytes may be read. C6385

C6305

All of them in 63 row

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论