C++ 面向对象问题-派生类
我试图找出对Class::Function()的未定义引用。这个项目是一个我看到了一些回应的项目,我也看到了其他项目的回应,看起来代码是相似的,但我似乎找不到我的具体问题的答案。我正在尝试使用2.h文件、它们的实现文件和驱动程序来简单地从一副牌中发牌。我遇到了一个难题,因为我不能100%确定如何使用派生类。我当前在编译时的错误如下:C++ 面向对象问题-派生类,c++,oop,derived-class,undefined-reference,playing-cards,C++,Oop,Derived Class,Undefined Reference,Playing Cards,我试图找出对Class::Function()的未定义引用。这个项目是一个我看到了一些回应的项目,我也看到了其他项目的回应,看起来代码是相似的,但我似乎找不到我的具体问题的答案。我正在尝试使用2.h文件、它们的实现文件和驱动程序来简单地从一副牌中发牌。我遇到了一个难题,因为我不能100%确定如何使用派生类。我当前在编译时的错误如下: 对“Deck::shuffleCards()”的未定义引用。 对“Deck::dealCard()”的未定义引用。 对“Card::getCard()const”
- 对“Deck::shuffleCards()”的未定义引用。
- 对“Deck::dealCard()”的未定义引用。
- 对“Card::getCard()const”的未定义引用
#ifndef CARD_H
#define CARD_H
#include <string>
using namespace std;
class Card
{
private:
string face; //Used to store value of face(A-K)
string suit; //User to store value of suit(A/R/S/B)
public:
string getCard() const; //Returns the card.
//Constructor
Card(string cardFace, string cardSuit)
{face = cardFace; suit = cardSuit;}
//Default Constructor
Card()
{}
};
#endif // CARD_H
#ifndef DECK_H
#define DECK_H
#include "CARD.H"
#include <string>
using namespace std;
const int SIZE = 52; //Standard Size of Deck. Used for calculations
class Deck : private Card
{
private:
Card *deckOfCards; //Creates pointer for deck.
int currentCard; //Keeps track of the current card array location
public:
//Constructor for Deck. Creates a deck of cards.
Deck() : Card()
{
string faces[] = {"Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
string suits[] = {"Acorns", "Shields", "Bells", "Roses"};
deckOfCards = new Card[SIZE];
currentCard = 0;
for(int i = 0; i < SIZE; i++)
{
deckOfCards[i] = Card(faces[i % 13], suits[ i / 13]);
}
}
void shuffleCards(); //Shuffles the cards.
Card dealCard(); //Returns a Card data type when this is called.
void printDeck() const; //Prints the deck.
};
#endif // DECK_H
\ifndef卡
#定义卡片
#包括
使用名称空间std;
班级卡
{
私人:
字符串face;//用于存储face的值(A-K)
字符串suit;//用于存储suit值的用户(A/R/S/B)
公众:
字符串getCard()const;//返回卡片。
//建造师
卡片(线卡面、线卡套)
{face=cardFace;suit=cardSuit;}
//默认构造函数
卡片()
{}
};
#endif//CARD_H
Card.cpp
#include <iostream>
#include "CARD.H"
string Card::getCard() const
{
//Returns the card in a format of X of Y(Ace of Shields)
return (face + " of " + suit);
}
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "DECK.H"
#include "CARD.H"
/**********************************************************************
* Uses 2 arrays to shuffle the cards. The function uses
* 2 arrays and random number generation with a modulus of the size of
* the deck to find the next location of the card.
**********************************************************************/
void Deck::shuffleCards()
{
currentCard = 0;
for(int first = 0; first < SIZE; first ++)
{
int second = (rand() + time(0)) % SIZE;
Card temp = deckOfCards[first];
deckOfCards[first] = deckOfCards[second];
deckOfCards[second] = temp;
}
}
Card Deck::dealCard()
{
//Checks to make sure the current card location is not greater than size.
//If so, this would indicate the deck needs to be shuffled because we're
// out of cards
if (currentCard > SIZE)
shuffleCards();
if (currentCard < SIZE)
return(deckOfCards[currentCard++]);
return deckOfCards[0];
}
//Function used to format the printing of the deck.
void Deck::printDeck() const
{
for(int i = 0; i < SIZE; i++)
{
cout << deckOfCards[i].getCard();
if((i + 1) % 5 == 0) //Prints 5 cards per line.
{
cout << endl; //If mod expression is valid, starts new line.
}
}
}
#包括
#包括“CARD.H”
字符串卡::getCard()常量
{
//以X/Y(盾牌王牌)的格式返回卡
返回(正面+正面+西装);
}
DECK.H
#ifndef CARD_H
#define CARD_H
#include <string>
using namespace std;
class Card
{
private:
string face; //Used to store value of face(A-K)
string suit; //User to store value of suit(A/R/S/B)
public:
string getCard() const; //Returns the card.
//Constructor
Card(string cardFace, string cardSuit)
{face = cardFace; suit = cardSuit;}
//Default Constructor
Card()
{}
};
#endif // CARD_H
#ifndef DECK_H
#define DECK_H
#include "CARD.H"
#include <string>
using namespace std;
const int SIZE = 52; //Standard Size of Deck. Used for calculations
class Deck : private Card
{
private:
Card *deckOfCards; //Creates pointer for deck.
int currentCard; //Keeps track of the current card array location
public:
//Constructor for Deck. Creates a deck of cards.
Deck() : Card()
{
string faces[] = {"Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
string suits[] = {"Acorns", "Shields", "Bells", "Roses"};
deckOfCards = new Card[SIZE];
currentCard = 0;
for(int i = 0; i < SIZE; i++)
{
deckOfCards[i] = Card(faces[i % 13], suits[ i / 13]);
}
}
void shuffleCards(); //Shuffles the cards.
Card dealCard(); //Returns a Card data type when this is called.
void printDeck() const; //Prints the deck.
};
#endif // DECK_H
#如果没有甲板#
#定义甲板
#包括“CARD.H”
#包括
使用名称空间std;
常数int SIZE=52//甲板的标准尺寸。用于计算
类别:私人卡
{
私人:
卡片*deckOfCards;//为卡片组创建指针。
int currentCard;//跟踪当前卡阵列位置
公众:
//一副牌的构造函数。创建一副牌。
卡片组():卡片组()
{
字符串面[]={“Ace”、“二”、“三”、“四”、“五”、“六”,
“七”、“八”、“九”、“十”、“杰克”、“女王”、“国王”};
弦乐套装[]={“橡子”、“盾”、“铃铛”、“玫瑰”};
deckOfCards=新卡[尺寸];
当前卡=0;
对于(int i=0;i
Deck.cpp
#include <iostream>
#include "CARD.H"
string Card::getCard() const
{
//Returns the card in a format of X of Y(Ace of Shields)
return (face + " of " + suit);
}
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "DECK.H"
#include "CARD.H"
/**********************************************************************
* Uses 2 arrays to shuffle the cards. The function uses
* 2 arrays and random number generation with a modulus of the size of
* the deck to find the next location of the card.
**********************************************************************/
void Deck::shuffleCards()
{
currentCard = 0;
for(int first = 0; first < SIZE; first ++)
{
int second = (rand() + time(0)) % SIZE;
Card temp = deckOfCards[first];
deckOfCards[first] = deckOfCards[second];
deckOfCards[second] = temp;
}
}
Card Deck::dealCard()
{
//Checks to make sure the current card location is not greater than size.
//If so, this would indicate the deck needs to be shuffled because we're
// out of cards
if (currentCard > SIZE)
shuffleCards();
if (currentCard < SIZE)
return(deckOfCards[currentCard++]);
return deckOfCards[0];
}
//Function used to format the printing of the deck.
void Deck::printDeck() const
{
for(int i = 0; i < SIZE; i++)
{
cout << deckOfCards[i].getCard();
if((i + 1) % 5 == 0) //Prints 5 cards per line.
{
cout << endl; //If mod expression is valid, starts new line.
}
}
}
#包括
#包括
#包括
#包括“DECK.H”
#包括“CARD.H”
/**********************************************************************
*使用2个阵列洗牌。函数使用
*2数组和随机数生成,模数大小为
*牌组以查找卡的下一个位置。
**********************************************************************/
空甲板::shuffleCards()
{
当前卡=0;
for(int first=0;first大小)
shuffleCards();
if(当前卡<尺寸)
返回(deckOfCards[currentCard++]);
返回卡[0];
}
//用于格式化数据组打印的函数。
void Deck::printDeck()常量
{
对于(int i=0;i 无法确保您正在将.cpp文件添加到makefile/project。这是一个编译错误,不是语法错误,与派生类无关。这可能是因为没有创建.o文件,因此编译器无法链接它们。发布makefile会很有用。我实际上是在使用代码块进行编译。还有其他建议吗那太棒了。