2013-09-16 3 views
-1

방금 ​​Bag 컨테이너에 소개되었으며 도움이 필요합니다. 가방 컨테이너를 사용하는 방법을 보여주는 코드를 받았습니다. 문제는 강사가 제공 한 코드에 파악할 수없는 오류가 있다는 것입니다. 새 가방을 만들 때 오류가 발생합니다. 다음은 헤더 파일과 실제 CPP 파일가방 컨테이너로 가방을 만드는 방법

#include <iostream> // For cout and cin 
#include <string> // For string objects 
#include "BagInterface.h"// For ADT bag 
#include <vector> 
#include <string> 
using namespace std; 
int main() 
{ 
string clubs[] = { "Joker", "Ace", "Two", "Three", 
"Four", "Five", "Six", "Seven", 
"Eight", "Nine", "Ten", "Jack", 
"Queen", "King" }; 
// Create our bag to hold cards 
Bag <string> grabBag;     //<<<<<<<<<<<<error is on this line 
// Place six cards in the bag 
grabBag.add(clubs[1]); 
grabBag.add(clubs[2]); 
grabBag.add(clubs[4]); 
grabBag.add(clubs[8]); 
grabBag.add(clubs[10]); 
grabBag.add(clubs[12]); 
// Get friend's guess and check it 
int guess = 0; 
while (!grabBag.isEmpty()) 
{ 
cout << "What is your guess?" 
<< "(1 for Ace to 13 for King):"; 
cin >> guess; 
// Is card in the bag? 
if (grabBag.contains(clubs[guess])) 
{ 
// Good guess – remove card from the bag 
cout << "You get the card!\n"; 
grabBag.remove(clubs[guess]); 
} 
else 
{ 
cout << "Sorry, card was not in the bag.\n"; 
} // end if 
} // end while 
cout << "No more cards in the bag.\n"; 
return 0; 
}; // end main 

/** @file BagInterface.h */ 
#ifndef _BAG_INTERFACE 
#define _BAG_INTERFAE 
#include <vector> 
template<class ItemType> 
class BagInterface 
{ 
public: 
/** Gets the current number of entries in this bad. 
@return The integer number of entries currently in the bag */ 
virtual int getCurrentSize() const = 0; 
/** Sees whether this is empty 
@return True if the bag is empty, or false if not */ virtual bool isEmpty() const = 0; 
/** Adds a new entry to this bag 
@post If successful, newEntry is stored in the bag and 
the count of items in the bag has increased by 1 
@param newEntry The object to be added as an new entry 
@return True is addition was successful, or false if not */ 
virtual bool add(const ItemType& newEntry) = 0; 
/** Removes one occurrence of a given entry from this bag, 
if possible 
@post If successful, anEntry has been removed from the bag 
and the count of items in the bad has decreased by 1. 
@param anEntry The entry to be removed 
@return True if removal was successful, or false if not */ 
virtual bool remove(const ItemType& anEntry) = 0; 
/** Removes all entries from this bag 
@post Bag contains no items, and the count of items is 0 */ 
virtual void clear() = 0; 
/** Counts the number of times a given entry appears in bag. 
@param anEntry The entry to be counted 
@return The numer of times anEntry appears in the bag */ 
virtual int getFrequencyOf(const ItemType& anEntry) const = 0; 
/** Tests whether this bag contains an given entry 
@param anEntry The entry to locate 
@return True if bag contains anEntry, or false otherwise */ 
virtual bool contains(const ItemType& anEntry) const = 0; 
/** Empties and then fills a given vector with all entries that 
are in this bag 
@return A vector containing all the entries in the bag */ 
virtual vector<ItemType> toVector() const = 0; 
}; // end BagInterface 

나는 이것에 붙어있어 그것을 작동하게하는 방법을 알고하지 않는 헤더 파일입니다. 감사합니다.

+4

오 신 :

은 코멘트에 링크 된 '더미'구현을 참조하십시오! 이 코드는 무엇입니까 ??? –

+0

또한 헤더 가드에는 오타가 있기 때문에 작동하지 않습니다 : _BAG_INTERFAE – sehe

+2

'std :: multiset'을 구출 하시겠습니까? – TemplateRex

답변

2

"인터페이스"(abstract class)를 받았습니다.

template<class ItemType> 
class BagInterface 
{ 
public: 
    virtual int getCurrentSize() const = 0; 
    virtual bool isEmpty() const = 0; 
    virtual bool add(const ItemType& newEntry) = 0; 
    virtual bool remove(const ItemType& anEntry) = 0; 
    virtual void clear() = 0; 
    virtual int getFrequencyOf(const ItemType& anEntry) const = 0; 
    virtual vector<ItemType> toVector() const = 0; 
}; // end BagInterface 

당신은 그것을 인스턴스화하고 의견에 따라 메소드를 구현해야합니다 (또는 아마 당신은 당신이 보여주지 않았다,뿐만 아니라 Bag.hpp/Bag.cpp 입수했습니다/포함 깜빡)! 프로그램 인쇄

What is your guess?(1 for Ace to 13 for King): 1 
You get the card! 
What is your guess?(1 for Ace to 13 for King): 2 
You get the card! 
What is your guess?(1 for Ace to 13 for King): 4 
You get the card! 
What is your guess?(1 for Ace to 13 for King): 8 
You get the card! 
What is your guess?(1 for Ace to 13 for King): 10 
You get the card! 
What is your guess?(1 for Ace to 13 for King): 12 
You get the card! 
No more cards in the bag. 
+0

나는 그가 그것을 인스턴스화하려고 시도하고 실패했다고 주장 할 것이다. 그는이 추상 인터페이스를 파생 클래스에서 실현해야합니다. 클래스 Bag : public BagInterface {} – AChampion

+0

@achampion 예, "Bag"컨테이너의 의미는 ... 정말 엉망입니다. – sehe

+0

@sehe 다른 Bagg.hpp 또는 Bag.cpp 파일은 없습니다. 하지만 클래스를 인스턴스화하고 추상화하는 방법은 무엇입니까? 나는 그것이 불가능하다고 생각한다. 오, 오류는 cpp 라인에 있습니다. Bag grabBag; –

관련 문제