在函数中... main.cpp :(。text + 0x7d6):未定义的引用

时间:2015-10-05 19:29:11

标签: c++ pointers compiler-errors undefined-reference

我是C ++的新手,我在下面写了一些代码。我试过不同的帖子,但找不到我的错误的解决方案。

我收到编译错误:

`/tmp/ccW0BytG.o: In function   bagTesterHW(BagInterface<std::string>*):main.cpp:(.text+0x7d6): undefined reference to BagInterface<std::string>::Union(BagInterface<std::string>*)`

我能够将错误与main.cpp程序中的以下代码隔离开来:

bagPtr3  = bagPtr->Union(bagPtr2);

我已经检查过并且所有的include语句都是正确的。我不知道“未定义”的来源。

任何帮助都会很棒。

Main.cpp方法

#include "BagInterface.h" 
#include "ArrayBag.h" 
#include "LinkedBag.h" 
#include <iostream> 
#include <string> 
#include <cctype>

using namespace std;

void displayBag(BagInterface<string>* bagPtr)
{
cout << "The bag contains " << bagPtr->getCurrentSize()
    << " items:" << endl;
vector<string> bagItems;
bagItems = bagPtr->toVector();
int numberOfEntries = bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
    cout << bagItems[i] << " ";
} // end for 
cout << endl << endl;
} // end displayBag 

void bagTesterHW(BagInterface<string>* bagPtr)
{
cout << "-----------bagTesterHW---------" << endl; 

//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
In function `bagTesterHW(BagInterface<std::string>*)': main.cpp:(.text+0x7d6): undefined reference to `BagInterface<std::string>::Union(BagInterface<std::string>*)'collect2: error: ld returned 1 exit status
*/
bagPtr3  = bagPtr->Union(bagPtr2); 

displayBag(bagPtr3); 

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

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

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

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;

...

/** 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 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;
// end BagInterface

/** 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

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

// Returns either a pointer to the node containing a given entry
// or the null pointer if the entry is not in the bag.
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

LinkedBag.cpp(联盟方法)

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

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

0 个答案:

没有答案