C++ 中的经典链接器 Mach 错误

Classic Linker Mach error in C++

我不太了解 C++,我正在自学 ADT 和数据结构。不幸的是,每次我需要编译代码时,我都会遇到经典的 xcode 错误:mach link 错误。我倾向于在 objective-c 中理解这些错误(因为这通常意味着我丢失了一个文件)。我确保将文件添加到我的目标以避免此问题,但这似乎不起作用。经典的打开和关闭 Xcode 技巧也没有。

错误及代码如下:

Undefined symbols for architecture x86_64:
  "ArrayBag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::getIndexOf(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const", referenced from:
      ArrayBag<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::remove(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.o
  "ArrayBag<int>::getIndexOf(int const&) const", referenced from:
      ArrayBag<int>::remove(int const&) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

#ifndef _ARRAY_BAG
#define _ARRAY_BAG
#include "BagInterface.h"
#include <vector>


template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
private:
    static const int DEFAULT_CAPACITY = 24; // the bag size a bit bigger to allow
                                            // adding bags.
    ItemType items[DEFAULT_CAPACITY];      // Array of bag items
    int itemCount;                         // Current count of bag items
    int maxItems;                          // Max capacity of the bag

    // Returns either the index of the element in the array items that
    // contains the given target or -1, if the array does not contain
    // the target.
    int getIndexOf(const ItemType& target) const;

public:
    ArrayBag();
    int getCurrentSize() const;
    bool isEmpty() const;
    bool add(const ItemType& newEntry);
    void clear();
    bool remove(const ItemType& anEntry);
    bool contains(const ItemType& target) const;
    int getFrequencyOf(const ItemType& anEntry) const;
    ArrayBag<string> merge(ArrayBag<string> a, ArrayBag<string> b);
    std::vector<ItemType> toVector() const;





#include "ArrayBag.h"
#include <cstddef>

template<class ItemType>
ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{
}  // end default constructor

template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
    return itemCount;
}  // end getCurrentSize

template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
    return itemCount == 0;
}  // end isEmpty

template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newEntry)
{
    bool hasRoomToAdd = (itemCount < maxItems);
    if (hasRoomToAdd)
    {
        items[itemCount] = newEntry;
        itemCount++;
    }  // end if

    return hasRoomToAdd;
}  // end add

template<class ItemType>
void ArrayBag<ItemType>::clear()
{
    itemCount = 0;
}  // end clear

template<class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& anEntry)
{
    int locatedIndex = getIndexOf(anEntry);
    bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
    if (canRemoveItem)
    {
        itemCount--;
        items[locatedIndex]=items[itemCount];
    }//end if
    return canRemoveItem;
} //end remove

template<class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const
{
    bool found = false;
    int curIndex = 0; //current array index
    while(!found && (curIndex < itemCount))
    {
        if(anEntry == items[curIndex])
        {
            found = true;
        } //end if

        curIndex++; //increment to the next entry
    } //end while
    return found;
}

template<class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
    int frequency = 0;
    int curIndex = 0; //current index array
    while(curIndex < itemCount)
    {
        if (items[curIndex] == anEntry)
        {
            frequency++;
        } //end if

        curIndex++; //incremenet to next entry
    } //end while
    return frequency;
} //end getFrequencyOf


template<class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const
{
    vector<ItemType> bagContents;
    for (int i = 0; i < itemCount; i++)
        bagContents.push_back(items[i]);

    return bagContents;
}  // end toVector

#include <iostream>
#include <string>
#include "ArrayBag.h"
#include "ArrayBag.cpp"

using namespace std;

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

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

int sumOfBag(ArrayBag<int>& aBag)
{
    int sum=0;
    vector<int> aBagvector = aBag.toVector();
    int numberOfEntries = (int) aBagvector.size();
    for (int i = 0; i < numberOfEntries; i++)
    {
        sum += aBagvector[i];
    }
    //cout << "The sum of the bag is " << sum << endl;
    return sum;
}

void bagTester(ArrayBag<string>& bag)
{
    cout << "isEmpty: returns " << bag.isEmpty()
    << "; should be 1 (true)" << endl;
    displayBag(bag);

    string items[] = {"one", "two", "three", "four", "five", "one"};
    cout << "Add 6 items to the bag: " << endl;
    for (int i = 0; i < 6; i++)
    {
        bag.add(items[i]);
    }  // end for

    displayBag(bag);

    cout << "isEmpty: returns " << bag.isEmpty()
    << "; should be 0 (false)" << endl;

    cout << "getCurrentSize: returns " << bag.getCurrentSize()
    << "; should be 6" << endl;

    cout << "Try to add another entry: add(\"extra\") returns "
    << bag.add("extra") << endl;
    displayBag(bag);
} // end bagTester


int main()
{
    ArrayBag<string> bag;
    cout << "Testing the Array-Based Bag:" << endl;
    cout << "The initial bag is empty." << endl;
    bagTester(bag);
    ArrayBag<string> bag_of_strings1;
    string items[] = {"tom","dick","harry"};
    for (int i=0; i<3; i++) {bag_of_strings1.add(items[i]);}
    displayBag(bag_of_strings1);
    ArrayBag<string> bag_of_strings2;
    string new_items[] = {"now","is","the","time","for","all"};
    for(int i=0; i<6; i++){
        bag_of_strings2.add(new_items[i]);
    }
    displayBag(bag_of_strings2);
    //ArrayBag<string> newbag=merge(bag_of_strings1,bag_of_strings2);

    //displayBag(newbag);

    ArrayBag<int> bag_of_ints;
    int array_of_ints[]={6,7,85,9,12,15};
    for (int i=0;i<6;i++){
        bag_of_ints.add(array_of_ints[i]);
    }
    displayBag(bag_of_ints);
    cout<<sumOfBag(bag_of_ints)<<endl;

    return 0;
} // end main

链接器告诉您 ArrayBag::getIndexOf 没有实现。您的代码转储中也缺少该实现。尝试添加:

int ArrayBag<ItemType>::getIndexOf(const ItemType& target) const
{
    // TODO
    return 0;
}

与正常 类 不同,模板 类 应在单个头文件中定义,而无需相应的 .cpp 文件。