因此,我們現在正在爲班級分配一個項目,並且我試圖弄清楚如何正確深度複製一系列卡片。下面我將發佈整個.cpp和.h的文件,我相當肯定它在operator =函數中,但我真正想要的只是如何解決它的一些提示。矢量是不允許使用的,也不是字符串。這是一個初級班,所以我們不太瞭解。我收到的主要錯誤是_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)請提前致謝。運營商=超載
//.h
#pragma once
#include "stdafx.h"
#include "Card.h"
class Player
{
private:
// Needed for unit tests
// DO NOT REMOVE OR RENAME
char* m_name; // Can change size of array if needed
Card* m_hand; // Can change size of array if needed
int m_numCards; // The number of cards the player is currently holding
int m_maxCards; // The number of cards the player can store (the number of elements in Hand)
int m_score;
public:
Player(const char* _name = "Player", int _maxCards = 5);
Player(const Player& _cpy);
virtual ~Player();
Player& operator=(const Player& _assign);
const char* GetName() const { return m_name; }
bool GetCard(int, Card&) const;
int GetNumCards() const { return m_numCards; }
int GetMaxCards() const { return m_maxCards; }
int GetScore() const { return m_score; }
/*MUTATORS*/
void SetName(const char* _name);
void AddToScore(int addScore);
bool AddCard(Card);
bool Discard(int index, Card&);
void Clear();
virtual void Show() const;
// Needed for unit tests
// DO NOT REMOVE
friend class CTestManager;
};
//.cpp
#include "Player.h"
Player::Player(const char* _name, int _maxCards){
m_name = NULL;
SetName(_name);
m_hand = NULL;
m_hand = new Card[_maxCards];
m_maxCards = _maxCards;
m_numCards = 0;
m_score = 0;
}
Player::Player(const Player& _cpy)
{
m_name = NULL;
SetName(_cpy.m_name);
m_hand = NULL;
m_hand = new Card(*_cpy.m_hand);
m_maxCards = _cpy.m_maxCards;
m_numCards = _cpy.m_numCards;
m_score = _cpy.m_score;
}
Player::~Player(){
delete[] m_name;
delete[] m_hand;
}
Player& Player::operator=(const Player& _assign)
{
if (this != &_assign)
{
delete[] m_name;
SetName(_assign.m_name);
delete[] m_hand;
//enter implemented deep copy here//
m_maxCards = _assign.m_maxCards;
m_numCards = _assign.m_numCards;
m_score = _assign.m_score;
}
return *this;
}
bool Player::GetCard(int index, Card& _Card) const{
if (index < m_numCards && index >= 0){
_Card = m_hand[index];
return true;
}
return false;
}
/*MUTATORS*/
void Player::SetName(const char* _name){
delete[] m_name;
int len = strlen(_name) + 1;
m_name = new char[len];
strcpy_s(m_name, len, _name);
}
void Player::AddToScore(int addScore){
m_score += addScore;
}
bool Player::AddCard(Card _addCard){
if (m_numCards != m_maxCards){
m_hand[m_numCards++] = _addCard;
return true;
}
return false;
}
bool Player::Discard(int discardIndex, Card& _discardCard){
if (discardIndex >= m_numCards){
return false;
}
else{
_discardCard = m_hand[discardIndex];
for (int i = discardIndex; i < m_maxCards - 1; i++){
m_hand[i] = m_hand[i + 1];
}
m_numCards--;
return true;
}
}
void Player::Clear(){
m_numCards = 0;
}
void Player::Show() const{
}
如果有任何提示,任何人都可以給我,我會非常感激:D再次感謝。
成語*](http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-and-swap)。 – juanchopanza
有關建議,請參閱此[常見問題](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) – 2014-01-17 06:53:42