File linking/coding problem
Apr 11, 2014 at 6:51pm UTC
This implementation file
VectorBag.cpp
is linked to a header file
VectorBag.h
which declares all of the variables and functions found here.
VectorBag.h
also has
#include "VectorBag.cpp
in it, so they both
#include
on each other.
VectorBag.cpp
in turn is supposed to connect to
proj2.cpp
which operated based on the functions in the prior. The problem I'm having here is that every variable/function used in
VectorBag.cpp
that is also in
VectorBag.h
gives an error message of being
previously declared . It looks something like this:
1 2 3 4
In file included from VectorBag.h:36:0,
from VectorBag.cpp:1:
VectorBag.cpp:99:5: error: \u2018int VectorBag<ItemType>::getIndexOf(const ItemType&) const \u2019 previously declared here
int VectorBag<ItemType>::getIndexOf(const ItemType& target) const {
Like that except for every variable/function. Any help?
Here's
VectorBag.cpp
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include "VectorBag.h"
#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) {
ItemType& temp;
temp = anEntry;
anEntry = items.back();
items.back() = temp;
items.pop_back();
return true ;
}
template <class ItemType>
void VectorBag<ItemType>::clear() {
items.clear();
}
template <class ItemType>
bool VectorBag<ItemType>::contains(const ItemType& anEntry) const {
bool found = false ;
int i = 0;
while (!found && (i < items.size())) {
if (anEntry == items[i])
found = true ;
i++;
}
return found;
}
template <class ItemType>
int VectorBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const {
int f = 0;
for (int i = 0; i < items.size(); i++) {
if (items[i] == anEntry)
f++;
}
return f;
}
template <class ItemType>
vector<ItemType> VectorBag<ItemType>::toVector() const {
for (int i = 0; i < items.size(); i++)
items.push_back(items[i]);
return items;
}
template <class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator +(VectorBag<ItemType> anotherBag) {
VectorBag<ItemType> newBag;
for (int i = 0; i < anotherBag.size(); i++)
newBag.add(anotherBag[i]);
}
template <class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator *(VectorBag<ItemType> anotherBag) {
VectorBag<ItemType> newBag;
for (int i = 0; i < anotherBag.size(); i++) {
if (anotherBag.contains(anotherBag[i]))
newBag.add(anotherBag[i]);
i++;
}
}
template <class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator -(VectorBag<ItemType> anotherBag) {
VectorBag<ItemType> newBag;
for (int i = 0; i < newBag.size(); i++) {
if (anotherBag.contains(newBag[i])) {
newBag.remove(newBag[i]);
anotherBag.remove(newBag[i]);
}
}
}
template <class ItemType>
int VectorBag<ItemType>::getIndexOf(const ItemType& target) const {
int i = 0;
while (target != items[i])
i++;
if (items.contains(target) == false ) i = -1;
return i;
}
btw, I'm pretty bad with boolean functions so I might've really messed up on their return values.
Apr 11, 2014 at 7:00pm UTC
also has #include "VectorBag.cpp in it
Never include a .cpp file in a header. That's guaranteed to cause duplicate defined symbols anytime the header is included in more than one module.
Apr 11, 2014 at 7:02pm UTC
The problem is that my professor wrote the "VectorBag.h" file and so I can't change anything in it.
Topic archived. No new replies allowed.