'<'''<'''&quot的模板类错误''令牌”
Template Class error "Expected initializer before '<' token"
我正在尝试构建一个名为arraybag的类,而我的编译器遇到了麻烦。
课程的目标是能够在所有数组中吸收大多数简单的数据类型。
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
您的IDE试图直接编译ArrayBag.cxx
,因为编译器尚未看到模板类声明(因为ArrayBag.cxx
没有#include ArrayBag.h
)而失败。如果有一种方法告诉您的IDE不要尝试编译此源文件(默认情况下它是因为它是" .cxx"文件),请执行此操作。否则,就像大多数其他人一样,并将整个声明/定义arraybag放在一个标头文件中。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- &lt;&lt;操作员在C 中超载错误
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- '&lt;'''&lt;'''&quot的模板类错误''令牌”
- 在尝试超载&lt;&lt;时链接错误2005和1169操作员
- 错误:没有匹配'运算符<<"在'std::cout
- 过载<<比如cout错误
- 重载& lt; & lt;为什么我得到以下错误
- 重载ostream&时转换错误无效;operator< & lt;
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多
- 编译器错误,包括'<'代币
- 重载& lt; & lt;错误