错误:预期':'之前' BagInterface' &安培;在会员功能

时间:2015-10-03 23:44:25

标签: c++ compiler-errors

背景

我是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(摘要)snippit

#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

2 个答案:

答案 0 :(得分:0)

第一个问题

  

编译错误

 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;

以上看起来非常奇怪,无论是对人类还是编译器。您要做的是使用LinkedBag<ItemType>初始化tBagPtr - 因此您需要使用括号或大括号。

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

提议的修复

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

答案 1 :(得分:0)

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