0

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

3 回答 3

7

您的复制构造函数不进行任何复制:

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

一旦提供了自己的复制构造函数和复制赋值运算符,编译器生成的就会被禁止。如果你需要做一些复制,你必须实现它。

(当然,这适用于您的所有课程。)

于 2013-04-11T06:49:30.617 回答
5

如果您实现了复制构造函数和复制赋值运算符,那么您实际上必须实现复制。它不会为你完成。

于 2013-04-11T06:48:43.500 回答
2

在调用复制构造函数时,您当然必须将值分配给要创建的对象......

Potion::Potion(const Potion & copy)
{
   //assign the elements from copy to your object here
   std::cout << "Potion's copy constructor " << std::endl;
}

复制构造函数不会为您执行此操作。

于 2013-04-11T06:49:32.820 回答