-3
我在編譯此代碼時遇到問題。我一直在感到混亂的錯誤,我似乎無法解決。我究竟做錯了什麼?我無法獲得此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;
}
編譯器應該給你一個問題列表,有什麼問題?雖然你可能認爲你所得到的錯誤是垃圾,但我們可能會幫助你破譯它們。 –
有很多錯誤。我不知道如何糾正它們。 – user1535982
如果您編寫代碼的人不知道如何糾正它們,那麼intertube上的隨機陌生人也不太可能會這樣做。如果你寫了一堆代碼,並且只有在編寫代碼之後才試着編譯它,並且被大量錯誤嚇倒了,那就是問題所在:你應該編寫一小塊代碼,測試它,確保它的工作原理,然後才寫下一個。不要一次寫十頁代碼,然後坐在那裏,看看數百個錯誤,並試圖找出所有的錯誤。 –