我无法编译这个C ++代码

时间:2016-03-06 02:37:20

标签: c++

我在编译此代码时遇到问题。我一直在弄错我似乎无法破解的错误。我究竟做错了什么?

此代码用于计算设定差值,设置并集和设定给定值的交集。

#include <iostream>
#include <vector>


template<class ItemType>
int VectorBag<ItemType>::getCurrentSize() const {
    return items.size();
}

template<class ItemType>
bool VectorBag<ItemType>::isEmpty() const {
    return items.size() == 0;
}

template<class ItemType>
bool VectorBag<ItemType>::add(const ItemType& newEntry) {
    items.push_back(newEntry);
    return true;
}

template<class ItemType>
bool VectorBag<ItemType>::remove(const ItemType& anEntry) {
    for( vector<ItemType>::iterator iter = items.begin(); iter != items.end(); ++iter ) {
        if( *iter == anEntry )
        {
            items.erase( iter );
            return true;
        }
    }
    return false;
}

template<class ItemType>
void VectorBag<ItemType>::clear() {
    items.clear();
}

template<class ItemType>

bool VectorBag<ItemType>::contains(const ItemType& anEntry) {
    bool found = false;
    int i = 0;
    while (!found && (i < (int) items.size())) {
        if (anEntry == items[i])
        found = true;
        i++;
    }
    return found;
}

template<class ItemType>
int VectorBag<ItemType>::getFrequencyOf(const ItemType& anEntry) {
    int f = 0;
    for (int i = 0; i < (int) items.size(); i++) {
    if (items[i] == anEntry)
        f++;
    }
    return f;
}

template<class ItemType>
vector<ItemType> VectorBag<ItemType>::toVector() {
    vector<ItemType> vec;
    //copy all elements to new set and return
    for (int i = 0; i < (int) items.size(); i++)
        vec.push_back(items[i]);
    return vec;
}

template<class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag) {
    VectorBag<ItemType> newBag;

    //use all elements from both sets
    for (int i = 0; i < (int) items.size(); i++) {
        newBag.add(items[i]);
    }

    for (int i = 0; i < (int) anotherBag.items.size(); i++) {
        newBag.add(anotherBag.items[i]);
    }

    return newBag;
}

template<class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator*(VectorBag<ItemType> anotherBag) {
    VectorBag<ItemType> newBag;
    vector<ItemType> v3;
    //find intersection of sets
    sort(this->items.begin(), this->items.end());
    sort(anotherBag.items.begin(), anotherBag.items.end());

    set_intersection(this->items.begin(), this->items.end(), anotherBag.items.begin(), anotherBag.items.end(),back_inserter(v3));
    newBag.items = v3;
    return newBag;
}

template<class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator-(VectorBag<ItemType> anotherBag) {
    VectorBag<ItemType> newBag;
    //check if items in set1 exists in set2
    for (int i = 0; i < (int) items.size(); i++) {
        if (!anotherBag.contains(items[i])) {
            newBag.add(items[i]);
        }
    }
    return newBag;
}

1 个答案:

答案 0 :(得分:2)

通过查看您的错误消息,我认为这可能与问题Separate .h and .cpp files in template class implementation有关 和 Why can templates only be implemented in the header file?

基本上,您需要将整个模板代码放在头文件中,而不是单独的.cpp和.h文件。这除非您明确地实例化您打算在.cpp文件中使用的类型,如Why can templates only be implemented in the header file?中所述。

你也没有在你的cpp文件中包含标题,因为 Pete Becker 注意到你,我乍一看错过了。感谢他。

相关问题