第一次制作了一个跨越几个类的程序,但我在函数调用方面遇到了问题



我制作了一个小店主程序,允许玩家从该店主那里购买物品并将物品存储在玩家物品栏中。我是第一次使用 OOP 技术,遇到了一个无法解决的问题。

我遇到的问题是在"店主.cpp","购买物品"中,物品没有添加到玩家库存中。我正在做的函数调用有问题。但是,如果我在main中调用"Player.AddItem((",它可以正常工作。根据我的理解,我组织代码的方式是正确的吗?我觉得它是最具可读性的——至少对我来说是这样——尽管你可以随意说不是。

这是我到目前为止的代码。很抱歉代码很长,我不确定您可以在此处粘贴的数量是否有限制,但我认为最好全部显示。

提前非常感谢你。

主要

// Shop.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Player.h"
#include "ShopKeeper.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main()
{
    Player player; //The player
    ShopKeeper shopKeeper; //The shop keeper
    int responce; //Menu navigation
    std::cout << "Greetings " << player.GetName() << ". Feel free to browse my wares." << "n";
    std::cout << "1: Purchase Items. 2: Sell Items. 3: List Your Items. 4: Show Gold. 5: Exit" << "n";
    do
    {
        std::cin >> responce;
        switch (responce)
        {
        case 1:
            shopKeeper.PurchaseItem();
            break;
        case 2:
            shopKeeper.SellItem();
            break;
        case 3:
            player.ListInventory();
            break;
        case 4:
            std::cout << "You have " << player.GetGold() << " gold coins." << "n";
            break;
        case 5:
            std::cout << "Thank you for shopping." << "n";
            break;
        default:
            std::cout << "Please enter valid data." << "n";
            std::cout << "1: Purchase Items. 2: Sell Items. 3: List Your Items. 4: Show Gold. 5: Exit" << "n";
        }
    } while (responce != 5);
    /*
    //This works
    player.AddItem("Mace", 30);
    player.ListInventory();
    std::cout << player.GetGold();
    */
    //Keep window open
    std::string barn;
    std::cin >> barn;
    return 0;
}

店主.h

#pragma once
#include <string>
class ShopKeeper
{
private: 

public:
    void PurchaseItem(); //Shop keeper has player buy items from them
    void SellItem(); //Shop keeper sells item to player
    ShopKeeper();
    ~ShopKeeper();
};

店主.cpp

#include "stdafx.h"
#include "ShopKeeper.h"
#include "Player.h"
#include <iostream>
//Player purchases item from shop keeper
void ShopKeeper::PurchaseItem()
{
    Player player;
    int responce = 0; //Menu navigation
    std::cout << "1: Mace - 30 gold. 2: Bow - 50 gold. 3: Boots - 10 gold. 4: Bearskin - 75 gold. 5: Helmet - 25 gold." << "n";
    do
    {
        std::cin >> responce;
        switch (responce)
        {
        case 1:
            player.AddItem("Mace", 30);
            break;
        case 2:
            player.AddItem("Bow", 50);
            break;
        case 3:
            player.AddItem("Boots", 10);
            break;
        case 4:
            player.AddItem("Bearskin", 75);
            break;
        case 5:
            player.AddItem("Helmet", 25);
            break;
        default:
            std::cout << "Please enter valid data." << "n";
            std::cout << "1: Mace - 30 gold. 2: Bow - 50 gold. 3: Boots - 10 gold. 4: Bearskin - 75 gold. 5: Helmet - 25 gold." << "n";
        }
    } while (responce > 5 || responce < 1);
}
//Shop keeper sells item to player
void ShopKeeper::SellItem()
{
    Player player;
    int responce = 0;
    player.ListInventory();
    switch (responce)
    {
    case 1:
        player.SellItem(0, 20);
        break;
    case 2:
        player.SellItem(1, 20);
        break;
    case 3:
        player.SellItem(2, 20);
        break;
    case 4: 
        player.SellItem(3, 20);
        break;
    case 5:
        player.SellItem(4, 20);
        break;
    default:
        std::cout << "Please enter valid data." << "n";
        player.ListInventory();
    }
}
ShopKeeper::ShopKeeper()
{
}

ShopKeeper::~ShopKeeper()
{
}

玩家.h

#pragma once
#include <vector>
class Player
{
private:
    const int maxNumbItems = 5; //Maximum number of items that inventory can store
    int goldCoins = 150, //Amount of gold coins the player has
        numbOfItems = 0; //Number of con-current items player holds
    std::vector<std::string> inventory; //Players inventory
    std::string name = "Gorrex"; //Players name
public:
    std::string GetName(); //Get the players name
    std::string AddItem(std::string item, int itemPrice); // Add item to players inventory
    void Player::SellItem(int itemNum, int itemPrice); //Sell item 
    bool IsInventoryFull(); //Check to see if players inventory is full
    int InventoryCapacity(); //Get capacity of inventory
    int GetGold(); //Get players gold
    void ListInventory();

    Player();
    ~Player();
};

播放器.cpp

#include "stdafx.h"
#include "Player.h"
#include <iostream>
#include <ostream>
#include <string>
//Get the players name
std::string Player::GetName()
{
    return name;
}
//Add item to players inventory
std::string Player::AddItem(std::string item, int itemPrice)
{
    //Is players inventory not full?
    if (IsInventoryFull())
    {
        std::cout << "Inventory is full.";
    }
    else
    {
        //Can player afford item?
        if (goldCoins >= itemPrice)
        {
            goldCoins -= itemPrice;
            numbOfItems++;
            std::cout << "You have purchased " << item << "." << "n";
            inventory.push_back(item); //Add item to inventory
            return item;
        }
        //If player cant afford item 
        else
        {
            std::cout << "You cannot afford this item." << "n";
        }
    }
}
void Player::SellItem(int itemNum, int itemPrice)
{
    char responce;
    std::cout << "Are you sure you want to sell: " << inventory[itemNum] << "? 'y' - Yes. 'n' - No." << "n";
    std::cin >> responce;
    switch (responce)
    {
    case 'y':
        numbOfItems++;
        goldCoins += itemPrice;
        inventory.erase(inventory.begin() + itemNum);
        break;
    case 'n':
        std::cout << "That is ok." << "n"; 
        break;
    default:
        std::cout << "Please enter correct data." << "n";
    }
}


//Check to see if players inventory is full
bool Player::IsInventoryFull()
{
    //If players inventory isnt full
    if (numbOfItems < maxNumbItems)
    {
        return false;
    }
    //If players inventory is full
    else
    {
        return true;
    }
}
//Return size of players inventory
int Player::InventoryCapacity()
{
    return inventory.size();
}
//Get the players gold
int Player::GetGold()
{
    return goldCoins;
}
//List the players inventory
void Player::ListInventory()
{
    int itemNumb = 0; //item number in menu
    for (int i = 0; i < inventory.size(); i++)
    {
        itemNumb++;
        std::cout << itemNumb << ": " << inventory[i] << "n";
    }
    /*  //If inventory is empty
    if (inventory.empty())
    {
        std::cout << "inventory is empty" << "n";
    }*/
}
Player::Player()
{
}

Player::~Player()
{
}

在 Shopkeeper 中,您声明的是函数调用后消失的Player的本地实例:

void ShopKeeper::PurchaseItem()
{
    Player player;

使PurchaseItem接受Player& player,而不是声明本地Player

SellItem也是如此。您需要指定向谁销售。

然后在您的main中可以这样说:

switch (responce)
{
case 1:
   shopKeeper.PurchaseItem(player);
   break;
 case 2:
    shopKeeper.SellItem(player);
//...

ShopKeeper 中的PlayerShopKeeper的本地,与main Player无关。

您必须将Player传递给ShopKeeper方法。您有几种选择:在构造函数中传递它ShopKeeper或通过引用传递给方法:

取代

void ShopKeeper::PurchaseItem()
{
    Player player;
    // ...

void ShopKeeper::PurchaseItem(Player& player)
{
    // ...

并称它

shopKeeper.PurchaseItem(player);

最新更新