Объект типа абстрактного класса & quot; set & lt; std :: string & gt; & quot; не разрешено:

Я использую Visual Studios, и когда я объявляю тип, он говорит:
«Ошибка: объект типа абстрактного класса»< std :: string> «не разрешено:
Чистая виртуальная функция «setinterface :: GetCurrentSize [with type = std :: string]» не имеет переопределения «

#include <iostream> // For cout and cin
#include <string>   // For string objects
#include "set.h"    // For ADT bag
#include "setinterface.h" // For ADT setinterface
#include <vector> // for vector objects
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.
set<string> grabBag;

К тому времени, когда я добираюсь, чтобы установить < строка> grabBag, когда я получаю эту ошибку. Вот остальная часть моего кода

set.h

#ifndef TEACH_CSCI235_BAGADT_BAG_H_
#define TEACH_CSCI235_BAGADT_BAG_H_

#include "setinterface.h"
template<class ItemType>
class set : public setinterface<ItemType>
{
public:
set(const ItemType& an_item);
int GetCurrentSize() const;
bool IsEmpty() const;
bool Add(const ItemType& new_entry);
bool Remove(const ItemType& an_entry);
void Clear();
bool Contains(const ItemType& an_entry) const;
vector<ItemType> ToVector() const;

private:
int GetIndexOf(const ItemType& target) const;
static const int kDefaultBagSize_ = 6;
ItemType items_[kDefaultBagSize_]; // array of bag items
int item_count_;                    // current count of bag items
int max_items_;                 // max capacity of the bag

// Returns either the index of the element in the array items that
// contains the given target or -1, if the array does not contain
// the target.
};  // end Bag#endif  // TEACH_CSCI235_BAGADT_BAG_H_template<class ItemType>
set<ItemType>::set() : item_count_(0), max_items_(kDefaultBagSize_)
{
return items_[item_count_];
}  // end default constructor

template<class ItemType>
int set<ItemType>::GetCurrentSize() const
{
return item_count_;
}  // end getCurrentSize

template<class ItemType>
bool set<ItemType>::IsEmpty() const
{
return item_count_ == 0;
}  // end isEmpty

template<class ItemType>
bool set<ItemType>::Add(const ItemType& new_entry)
{
bool has_room_to_add = item_count_ < max_items_;

//compares the new entry to every item in item_ and if there is a duplicate, the loop breaks and nothing is added.
if (has_room_to_add)
{
for ( int i =0; i < max_items_; i++)
{
if (items_[i] == new_entry)
break;  //ends loop

else if (i==5)
{
items_[item_count_] = new_entry;
item_count_++;
break;  //ends loop
}  // end if
} // end for
} //end if

return has_room_to_add;
}  // end add

template<class ItemType>
bool set<ItemType>::Remove(const ItemType& an_entry)
{
int located_index = GetIndexOf(an_entry);
bool can_remove_item = !IsEmpty() && (located_index > -1);
if (can_remove_item)
{
item_count_--;
items_[located_index] = items_[item_count_];
}  // end if
return can_remove_item;
}  // end remove

template<class ItemType>
void set<ItemType>::Clear()
{
item_count_ = 0;
}  // end cleartemplate<class ItemType>
bool set<ItemType>::Contains(const ItemType& an_entry) const
{
return GetIndexOf(an_entry) > -1;
}  // end contains

template<class ItemType>
vector<ItemType> set<ItemType>::ToVector() const
{
vector<ItemType> bag_contents;
for (int i = 0; i < item_count_; i++)
bag_contents.push_back(items_[i]);
return bag_contents;
}  // end toVector

template<class ItemType>
int set<ItemType>::GetIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int search_index = 0;
// if the bag is empty, item_count is zero, so loop is skipped
while (!found && (search_index < item_count_))
{
if (items_[search_index] == target)
{
found = true;
result = search_index;
}
else
{
search_index++;
}  // end if
}  // end while
return result;
}  // end getIndexOf

вот все из setinterface.h

#ifndef TEACH_CSCI235_BAGADT_BAGINTERFACE_H_
#define TEACH_CSCI235_BAGADT_BAGINTERFACE_H_

#include <vector>
#include <cstddef>
using namespace std;

template<class ItemType>
class setinterface
{
public:
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
virtual int GetCurrentSize() const = 0;

/** Sees whether this bag 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 new_entry  The object to be added as a new entry.
@return  True if addition was successful, or false if not. */
virtual bool Add(const ItemType& new_entry) = 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 bag has decreased by 1.
@param an_entry  The entry to be removed.
@return  True if removal was successful, or false if not. */
virtual bool Remove(const ItemType& an_entry) = 0;

/** Removes all entries from this bag.
@post  Bag contains no items, and the count of items is 0. */
virtual void Clear() = 0;

/** Tests whether this bag contains a given entry.
@param an_entry  The entry to locate.
@return  True if bag contains anEntry, or false otherwise. */
virtual bool Contains(const ItemType& an_entry) 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
#endif  // TEACH_CSCI235_BAGADT_BAGINTERFACE_H_

Я начал получать эту ошибку сразу после того, как попытался создать конструктор в классе set, и когда я пошел, чтобы сохранить его, он дал мне уведомление, что он собирается использовать что-то еще для сохранения моих данных, я не заплатил много внимание на это, но я думаю, что это как-то связано с этой ошибкой.
Я также предполагаю «добавить конструктор, который создает Set для одного элемента:» в соответствии с моим назначением, так что мой конструктор может быть хорошим местом для поиска. Я даже не уверен, правильно ли я это сделал.
Я ценю любую помощь. Спасибо!

0

Решение

Задача ещё не решена.

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector