错误:'BagInterface' 和 In 成员函数之前的预期':'

error: expected ':' before 'BagInterface' & In Member function

本文关键字:In BagInterface 错误 成员 函数      更新时间:2023-10-16

背景

我是C++的新手,我正在用C++创建一个对象,其中对象既可以是数组也可以是指针。我需要创建一个UNION方法,它可以组合两个BagInterface对象。我在更正代码以便使用指针进行编译时遇到了问题。如有任何帮助,我们将不胜感激。

编译错误:

 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>;

第一个问题

编译器错误

 BagInterface.h:64:9: error: expected ':' before 'BagInterface'

public BagInterface<ItemType>* Union(BagInterface<ItemType>* bagPtr);

C++不是java,public后面缺少一个冒号。


第二个问题

编译器错误

LinkedBag.cpp:116:26: error: expected unqualified-id before '=' token
BagInterface<ItemType>* = new LinkedBag<ItemType> tBagPtr;

对于人类和编译器来说,上面的内容看起来都很奇怪。您要做的是使用tBagPtr初始化LinkedBag<ItemType>—因此,您需要使用括号或大括号。

您还需要为要声明的对象提供一个名称。


建议的修复

 public:
     BagInterface<ItemType>* Union(BagInterface<ItemType>* bagPtr);
BagInterface * some_name = new LinkedBag (tBagPtr);