背景

我是C++的新手,正在努力用c++创建一个对象,该对象可以是数组或指针。我需要创建一个可以组合两个BagInterface对象的UNION方法。我在更正代码以使其可以使用指针进行编译时遇到了麻烦。任何帮助将不胜感激。

编译错误:

 In file included from main.cpp:3:0:
 BagInterface.h:64:9: error: expected ':' before 'BagInterface'
 In file included from LinkedBag.h:43:0,
             from main.cpp:5:
 LinkedBag.cpp: In member function 'BagInterface<ItemType>* LinkedBag<ItemType>::Union(BagInterface<ItemType>*)':
 LinkedBag.cpp:116:26: error: expected unqualified-id before '=' token
 LinkedBag.cpp:123:3: error: 'tBagPtr' was not declared in this scope
 LinkedBag.cpp:133:3: error: 'tBagPtr' was not declared in this scope
 LinkedBag.cpp:136:9: error: 'tBagPtr' was not declared in this scope

main.pp文件(方法):
void bagTesterHW(BagInterface<string>* bagPtr)
{
//Clear Bag To Set Up Test Cases
bagPtr->clear();

//Test Clear
//displayBag(bagPtr);

//Second Array Bag
BagInterface<string>* bagPtr2 = new LinkedBag<string>();
BagInterface<string>* bagPtr3 = new LinkedBag<string>();

//LinkedBag<string> bagPtr2;
//LinkedBag<string> bagPtr3;

//Test Case Strings
string items1[] = { "two", "two", "three", "four" };
string items2[] = { "two", "four" };

//Filling Linked List
for (int i = 0; i < 4; i++)
{
    bagPtr->add(items1[i]);
} // end for

for (int i = 0; i < 2; i++)
{
    //bagPtr2.add(items2[i]); //LinkedBag
    bagPtr2->add(items2[i]);
} // end for

displayBag(bagPtr);
displayBag(bagPtr2);

/*Problem Code
Union function not defined in Parent Object*/
bagPtr3 = bagPtr->Union(bagPtr2);

displayBag(bagPtr3);

/*Release Memory*/
bagPtr2 = nullptr;
bagPtr3 = nullptr;

/*Delete Pointers*/
delete bagPtr2;
delete bagPtr3;

cout << "--------end bagTesterHW---------" << endl;}

LinkedBag.cpp(方法)
template<class ItemType>
BagInterface<ItemType>* LinkedBag<ItemType>::Union(BagInterface<ItemType>* bagPtr)
{
BagInterface<ItemType>* = new LinkedBag<ItemType> tBagPtr;

vector<ItemType> bagItems;
bagItems = toVector();

for (int i = 0; i < bagItems.size(); i++)
{
    tBagPtr->add(bagItems[i]);
} // end for

//Clear Items
bagItems.clear();
bagItems = bagPtr->toVector();


for (int i = 0; i < bagItems.size(); i++)
{
    tBagPtr->add(bagItems[i]);
} // end for

return tBagPtr;
}

LinkedBag.h
#ifndef _LINKED_BAG
#define _LINKED_BAG

#include "BagInterface.h"
#include "Node.h"

template<class ItemType>
class LinkedBag : public BagInterface<ItemType>
{
private:
Node<ItemType>* headPtr; // Pointer to first node
int itemCount;           // Current count of bag items

Node<ItemType>* getPointerTo(const ItemType& target) const;

public:
LinkedBag();
LinkedBag(const LinkedBag<ItemType>& aBag); // Copy constructor
virtual ~LinkedBag();                       // Destructor should be virtual
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector<ItemType> toVector() const;
BagInterface<ItemType>* Union(BagInterface<ItemType>* bagPtr);

}; // end LinkedBag

#include "LinkedBag.cpp"
#endif

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;
. . .
/** Empties and then f ills 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;
 /** Creates a new bag that combines the contents of this bag and a
 second given bag without affecting the original two bags.
 @param anotherBag The given bag.
 @return A bag that is the union of the two bags. */
 //public BagInterface<ItemType> union(BagInterface<ItemType> anotherBag);
 public BagInterface<ItemType>* Union(BagInterface<ItemType>* bagPtr);
 .  .   .
 };
 #endif

最佳答案

C++在个别声明中不支持public。相反,您可以使用它标记类的整个部分:

class Foo {
    public:
        void foo();
        void bar();
    private:
        int baz();
};

这就是为什么编译器说期望:之后有public的原因。

另一个错误涉及此行:
BagInterface<ItemType>* = new LinkedBag<ItemType> tBagPtr;

就是说在=之前需要一个变量名。您可能打算写:
BagInterface<ItemType>* tBagPtr = new LinkedBag<ItemType>;

09-26 14:41