-3

I am having trouble compiling this code. I have been getting confusing errors that I can't seem to crack. What am I doing wrong?

This code is supposed to calculate the set difference, set union, and set intersection of given values.

#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;
}
user1535982
  • 43
  • 1
  • 3
  • The compiler should be giving you a list of issues, what are the problems? Although you might think that the errors you are getting are garbage, we probably can help you decipher them. – Sumner Evans Mar 06 '16 at 02:38
  • there are a lot of errors. I'm not sure how to correct them. – user1535982 Mar 06 '16 at 02:42
  • If you, who wrote the code, doesn't know how to correct them, it's unlikely that random strangers on the intertube would either. If you wrote a bunch of code, and only after writing it all you tried to compile it, and got intimidated by a ton of errors, that's what the problem is: you should be writing one small chunk of code, testing it, make sure it works, and only then write the next one. Don't write ten pages of code all at once, then sit there, look at the hundreds of errors, and try to figure out them all. – Sam Varshavchik Mar 06 '16 at 02:45
  • @user1535982, how do you expect us to fix the errors if we can't even see them? You might not know how to correct them, but we might. – Sumner Evans Mar 06 '16 at 02:47
  • here are some of the errors: VectorBag.cpp:7:5: error: redefinition of ‘int VectorBag::getCurrentSize() const’ int VectorBag::getCurrentSize() const { ^ In file included from VectorBag.h:29:0, from VectorBag.cpp:3: VectorBag.cpp:7:5: note: ‘virtual int VectorBag::getCurrentSize() const’ previously declared here int VectorBag::getCurrentSize() const { ^ VectorBag.cpp:12:6: error: redefinition of ‘bool VectorBag::isEmpty() const’ bool VectorBag::isEmpty() const { ^ – user1535982 Mar 06 '16 at 02:49
  • Neither `` nor `` (nor any other standard header) defines a template named `VectorBag`. If you have a header that does, you need to `#include` that header in order to use its contents. – Pete Becker Mar 06 '16 at 03:01

1 Answers1

2

By looking at your error messages, I believe this might be related to issue Separate .h and .cpp files in template class implementation and Why can templates only be implemented in the header file?

Basically, you need to put the whole template code in the header file and not have a separate .cpp and .h files. This unless you explicitly instantiate the type(s) you intend to use within the .cpp file itself, as explained in Why can templates only be implemented in the header file?.

You are also not including the header within your cpp file, as Pete Becker noted you and I missed at a first glance. Kudos to him.

Community
  • 1
  • 1
Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40