문제

I'm trying to build a class called ArrayBag, and I'm having trouble with my compiler giving me that error.

The goal of the class is to be able to take in most simple data types in a catch all array.

ArrayBag.cxx

//
#include <cstddef>

// Constructor; creates and initializes an empty Bag
template <class ItemType>
ArrayBag<ItemType>::ArrayBag()
{
    itemCount = 0;
}

// Return the number of Items being stored in the Bag
template <class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
    return itemCount;   // STUB
}

// Return the capacity of the bag (the maximum Items it can store) 
template <class ItemType>
int ArrayBag<ItemType>::getCapacity( ) const
{
    return DEFAULT_CAPACITY;
}

// Report whether the Bag is empty
// Return true if the Bag is empty (storing no Items);
// Return false if Items exist in the Bag
template <class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
    return (itemCount == 0);
}

// Report whether the Bag is full
// Return true if the Bag is filled to capacity
// Return false if there is still room
template <class ItemType>
bool ArrayBag<ItemType>::isFull() const
{
    return (itemCount == DEFAULT_CAPACITY); // STUB
}

// Give the Bag a new Item to store
// If Bag is full, nothing changes
// Else, Bag must add this Item to its Item array and update its itemCount
// If Bag is full after this, return true; else return false
template <class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newItem)
{
    if(itemCount == DEFAULT_CAPACITY)
    {
        cout << "The bag is full!" << endl;
        return false;
    }
    else
    {
        items[itemCount] = newItem;
        itemCount++;
        return true;
    }
}

// Make the Bag act like an empty Bag again
template <class ItemType>
void ArrayBag<ItemType>::clear()
{
    itemCount = 0;
}

// Remove an Item from the bag
// If Item is not there, nothing changes and we return false
// Else, we fill in its spot in that Item array and count number of Items down
template <class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& anItem)
{
    int index;
    for(int i=0; i<=itemCount;i++)
    {
        index = i;
        if(items[i] == anItem)
        {
            break;
        }
        else
        {
            cout << "Item not found in the bag bro." << endl;
            return false;
        }
    }
    for(int i=index;i<=itemCount;i++)
    {
        items[i] = items[i+1];
    }
    return true;
}

// Check if an Item is in the Bag
// Return true if it is in the Bag, and false if not
template <class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anItem) const
{
    for(int i = 0; i <= DEFAULT_CAPACITY; i++)
        if(items[i] == anItem){
            return true;
        }
        return false;
    // STUB
}

// Check how many times an Item is in the Bag
// return 0 if it's not there; otherwise,
// return the number of times it occurs
template <class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anItem) const
{
    return 0;       // STUB
}

// Make an output vector of Items from the bag (for checking)
template <class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const
{
    vector<ItemType> bagContents;
    // small STUB
    return bagContents;             
}

ArrayBag.h

#ifndef _ARRAY_BAG
#define _ARRAY_BAG

#include "BagInterface.h"
template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
    private:
        static const int DEFAULT_CAPACITY = 25;
        ItemType items[DEFAULT_CAPACITY];
        int itemCount;

    public:
        ArrayBag();
        int getCurrentSize() const;
        int getCapacity() const;
        bool isEmpty() const;
        bool isFull() const;
        bool add(const ItemType& newItem);
        bool remove(const ItemType& anItem);
        void clear();
        bool contains(const ItemType& anItem) const;
        int getFrequencyOf(const ItemType& anItem) const;
        vector<ItemType> toVector() const;
};

#include "ArrayBag.cxx"
#endif

BagInterface.h

//  Created by Frank M. Carrano and Tim Henry.
//  Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 1-1.
    @file BagInterface.h */
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE

#include <vector>
using namespace std;

template<class ItemType>
class BagInterface
{
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;

   /** Tells us the total capacity of the bag.
    @return DEFAULT CAPACITY. */
   virtual int getCapacity() const = 0;

   /** Sees whether this bag is full.
    @return True if the bag is full, or false if not. */
   virtual bool isFull() 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 a new entry.
    @return  True if 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 bag 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 number of times anEntry appears in the bag. */
   virtual int getFrequencyOf(const ItemType& anEntry) const = 0;

   /** Tests whether this bag contains a 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
#endif

How can I fix this? I don't see what I'm doing wrong.

Here are the errors.

Description Resource    Path    Location    Type
'ArrayBag' does not name a type ArrayBag.cxx    /ArrayBag   line 6  C/C++ Problem
'vector' does not name a type   ArrayBag.cxx    /ArrayBag   line 121    C/C++ Problem
expected initializer before '<' token   ArrayBag.cxx    /ArrayBag   line 114    C/C++ Problem
expected initializer before '<' token   ArrayBag.cxx    /ArrayBag   line 74 C/C++ Problem
expected initializer before '<' token   ArrayBag.cxx    /ArrayBag   line 100    C/C++ Problem
expected initializer before '<' token   ArrayBag.cxx    /ArrayBag   line 48 C/C++ Problem
expected initializer before '<' token   ArrayBag.cxx    /ArrayBag   line 65 C/C++ Problem
expected initializer before '<' token   ArrayBag.cxx    /ArrayBag   line 29 C/C++ Problem
expected initializer before '<' token   ArrayBag.cxx    /ArrayBag   line 38 C/C++ Problem
expected initializer before '<' token   ArrayBag.cxx    /ArrayBag   line 13 C/C++ Problem
expected initializer before '<' token   ArrayBag.cxx    /ArrayBag   line 20 C/C++ Problem
도움이 되었습니까?

해결책

Your IDE is trying to compile ArrayBag.cxx directly, which fails because the compiler has not yet seen the template class declaration (because ArrayBag.cxx does not #include ArrayBag.h). If there's a way to tell your IDE not to try compiling this source file (which it wants to by default because it's a ".cxx" file), then do that. Otherwise do like most other people do and put the entire declaration/definition of ArrayBag in a single header file.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top