OOP 有点新(即 C 程序员转换为 C++)并且无法弄清楚为什么我的背包类中的数据成员是空的。我将一系列药水传递到我的背包,但数据成员却说 mType = ""(即什么都没有)。
我以前从未在程序中感到这种迷失。开始讨厌 OOP(即开玩笑……但这非常令人沮丧)。
主文件
#include <iostream>
#include "rogue.h"
#include "weapon.h"
#include "backpack.h"
#include "potion.h"
#include "coinpouch.h"
int main()
{
Potion myFavoritePotions[5];
myFavoritePotions[0].setName("love");
myFavoritePotions[1].setName("hate");
myFavoritePotions[2].setName("shrink");
myFavoritePotions[3].setName("grow");
myFavoritePotions[4].setName("disappear");
BackPack myFavoriteBackPack(myFavoritePotions);
Weapon myFavoriteWeapon("AK-47");
Weapon mySecretWeapon("Me-262");
Weapon myLeastFavoriteWeapon("Luger");
CoinPouch myFavoritePurse(6,5,4,3);
Rogue myFavoriteRogue("Cynic", myFavoriteWeapon, mySecretWeapon, myFavoriteBackPack, myFavoritePurse);
mySecretWeapon = myFavoriteWeapon;
myFavoriteRogue.setOffHand(myLeastFavoriteWeapon);
//std::cout << myFavoriteRogue.getOffHand();
return 0;
}
药水.cpp
#include <iostream>
#include "potion.h"
//Manager function definitions
//Default constructor
Potion::Potion()
{}
//Constructor
Potion::Potion(std::string name)
:mName(name)
{
std::cout << "Potion's constructor " << std::endl;
}
//Destructor
Potion::~Potion()
{
std::cout << "Potion's destructor " << std::endl;
}
//Copy constructor
Potion::Potion(const Potion & copy)
{
std::cout << "Potion's copy constructor " << std::endl;
}
//Overloaded assignment operator
Potion &Potion::operator= (const Potion & rhs)
{
std::cout << "Potion's overloaded assignment operator. " << std::endl;
return *this;
}
//Setters
void Potion::setName(std::string name)
{
mName = name;
}
//Getters
std::string Potion::getName()
{
return mName;
}
背包.cpp
#include <iostream>
#include "backpack.h"
//Manager function definitions
//Default constructor
BackPack::BackPack()
{}
//Constructor
BackPack::BackPack(Potion Potions[])
{
for(int i = 0; i < 5; i++)
{
mPotions[i] = Potions[i];
}
std::cout << "Backpack's constructor. " << std::endl;
}
//Destructor
BackPack::~BackPack()
{
std::cout << "Backpack's destructor. " << std::endl;
}
//Copy constructor
BackPack::BackPack(const BackPack & copy)
{
std::cout << "Backpack's copy constructor. " << std::endl;
}
//Overloaded assignment operator
BackPack &BackPack::operator=(const BackPack & rhs)
{
std::cout << "Backpack's assignment operator. " << std::endl;
return *this;
}
//Setters
void BackPack::setPotion(Potion Potions[])
{
for(int i = 0; i < 5; i++)
{
mPotions[i] = Potions[i];
}
}
//Getters
Potion * BackPack::getPotion()
{
Potion * potionPointer = mPotions;
return potionPointer;
}