You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++类内物品库存数组的读写实现及相关报错问题解决

问题原因总结
  • 库存成员变量类型错误:当前定义的string inv是单个字符串,不符合4格库存的需求,应该改为长度为4的字符串数组string inv[4]
  • setInv方法逻辑完全错误:无入参无法接收要存入的格子位置和物品内容;方法内部定义的局部数组string inv[4]是临时变量,函数执行完就会销毁,赋值无效;访问inv[4]属于越界操作,数组长度为4时合法索引范围是0~3,访问索引4会触发内存溢出错误
  • getInv方法逻辑错误:无入参无法指定要读取的库存格子,返回值也对应错误的单个字符串类型
  • display方法的库存遍历逻辑无效:循环调用getInv()没有接收返回值也没有打印,自然看不到任何库存内容
修复方案
  1. 修改成员变量为4格字符串数组
  2. 重写setInv方法,接收索引和物品名两个参数,增加索引合法性校验避免越界
  3. 重写getInv方法,接收索引参数,返回对应格子的物品内容
  4. 修改display方法的库存打印逻辑,循环输出每个格子的物品内容
  5. 给测试玩家增加库存赋值的调用示例
完整修正代码
#pragma warning(disable: 4996) 
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

//player class
class player {
public:
    //name
    void setName(string name) { 
        this->name = name; 
    }
    string getName() {
        return name;
    }

    //password
    void setPass(string pass) {
        this->pass = pass;
    }
    string getPass() {
        return pass;
    }

    //experience
    void setXP(int xp) {
        this->xp = xp;
    }
    int getXP() {
        return xp;
    }

    //position
    void setPosX(int xPos) {
        this->xPos = xPos;
    }
    void setPosY(int yPos) {
        this->yPos = yPos;
    }
    int getPosX() {
        return xPos;
    }
    int getPosY() {
        return yPos;
    }
    
    //inventory 重写后的库存读写方法
    void setInv(int index, string item) {
        if (index >= 0 && index < 4) {
            inv[index] = item;
        }
    }
    string getInv(int index) {
        if (index >= 0 && index < 4) {
            return inv[index];
        }
        return "无效格子";
    }

    void display();

private:
    string name;
    string pass;
    string inv[4]; // 修改为4格字符串数组
    int xp = 0;
    int xPos = 0;
    int yPos = 0;
};

void player::display() {
    cout << "Player Info - \n";
    cout << "Name: " << getName() << "\n";
    cout << "Password: " << getPass() << "\n";
    cout << "Experience: " << getXP() << "\n";
    cout << "Position: " << getPosX() << ", " << getPosY() << "\n";
    cout << "Inventory: \n";
    for (int i = 0; i < 4; i++) {
        cout << "  格子" << i+1 << ": " << getInv(i) << "\n";
    }
    cout << "\n";
}
int main()
{
    //playerOne
    player playerOne;
    playerOne.setName("Porcupixel");
    playerOne.setPass("PokeHerFace2008");
    playerOne.setXP(1477);
    playerOne.setPosX(16884);
    playerOne.setPosY(10950);
    // 给playerOne设置库存
    playerOne.setInv(0, "铁剑");
    playerOne.setInv(1, "生命药水*3");
    playerOne.setInv(2, "木盾");
    playerOne.setInv(3, "传送卷轴");

    //playerTwo
    player playerTwo;
    playerTwo.setName("Commandroid");
    playerTwo.setPass("RodgerRodger00110001");
    playerTwo.setXP(73721);
    playerTwo.setPosX(6620);
    playerTwo.setPosY(36783);
    // 给playerTwo设置库存
    playerTwo.setInv(0, "能量步枪");
    playerTwo.setInv(1, "弹药包*5");
    playerTwo.setInv(2, "护甲片");
    playerTwo.setInv(3, "通讯器");

    //playerThree
    player playerThree;
    playerThree.setName("BumbleBeast");
    playerThree.setPass("AutoBotsRule7");
    playerThree.setXP(20641);
    playerThree.setPosX(15128);
    playerThree.setPosY(46976);
    // 给playerThree设置库存
    playerThree.setInv(0, "战斧");
    playerThree.setInv(1, "治疗喷雾");
    playerThree.setInv(2, "合金护甲");
    playerThree.setInv(3, "加速芯片");

    playerOne.display();
    playerTwo.display();
    playerThree.display();

    return 0;
}

内容的提问来源于stack exchange,提问作者user11495912

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 16:48:01