模板类错误“'之前的预期初始化程序'<'令牌”

时间:2014-01-28 23:18:46

标签: c++ templates initialization eclipse-cdt

我正在尝试构建一个名为ArrayBag的类,我的编译器遇到了这个错误。

该类的目标是能够在catch all数组中接收大多数简单数据类型。

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

我该如何解决这个问题?我不明白我做错了什么。

以下是错误。

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

1 个答案:

答案 0 :(得分:2)

您的IDE正在尝试直接编译ArrayBag.cxx,这会失败,因为编译器尚未看到模板类声明(因为ArrayBag.cxx不是#include ArrayBag.h)。如果有办法告诉你的IDE不要尝试编译这个源文件(默认情况下它想要它,因为它是一个“.cxx”文件),那就这样做吧。否则就像大多数人一样,将ArrayBag的整个声明/定义放在一个头文件中。