Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我能';我得不到这个C++;要编译的代码_C++ - Fatal编程技术网

C++ 我能';我得不到这个C++;要编译的代码

C++ 我能';我得不到这个C++;要编译的代码,c++,C++,我在编译此代码时遇到问题。我一直在犯令人困惑的错误,我似乎无法破解。我做错了什么 此代码用于计算给定值的集差、集并集和集交集 #include <iostream> #include <vector> template<class ItemType> int VectorBag<ItemType>::getCurrentSize() const { return items.size(); } template<class It

我在编译此代码时遇到问题。我一直在犯令人困惑的错误,我似乎无法破解。我做错了什么

此代码用于计算给定值的集差、集并集和集交集

#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;
}
#包括
#包括
样板
int VectorBag::getCurrentSize()常量{
返回items.size();
}
样板
布尔向量包::isEmpty()常量{
返回项目。大小()==0;
}
样板
bool向量包::添加(const ItemType和newEntry){
项目。推回(新条目);
返回true;
}
样板
bool向量包::删除(const ItemType和anEntry){
对于(向量::迭代器iter=items.begin();iter!=items.end();++iter){
if(*iter==入口)
{
项目。擦除(iter);
返回true;
}
}
返回false;
}
样板
void向量包::clear(){
items.clear();
}
样板
bool向量包::包含(const ItemType和anEntry){
bool-found=false;
int i=0;
而(!found&(i<(int)items.size()){
if(anEntry==项目[i])
发现=真;
i++;
}
发现退货;
}
样板
int VectorBag::getFrequencyOf(const ItemType和anEntry){
int f=0;
对于(int i=0;i<(int)items.size();i++){
如果(项目[i]==输入)
f++;
}
返回f;
}
样板
向量向量包::toVector(){
向量向量机;
//将所有元素复制到新集合并返回
对于(int i=0;i<(int)items.size();i++)
向量推回(项目[i]);
返回向量;
}
样板
矢量包
向量包::运算符+(向量包另一个){
矢量包;
//使用两个集合中的所有元素
对于(int i=0;i<(int)items.size();i++){
新增(第[i]项);
}
对于(int i=0;i<(int)另一个bag.items.size();i++){
新增(另一袋项目[i]);
}
返回新包;
}
样板
矢量包
向量包::操作员*(向量包另一个){
矢量包;
向量v3;
//求集合的交集
排序(this->items.begin(),this->items.end());
排序(anotherBag.items.begin(),anotherBag.items.end());
设置交叉点(this->items.begin(),this->items.end(),anotherBag.items.begin(),anotherBag.items.end(),back_inserter(v3));
newBag.items=v3;
返回新包;
}
样板
矢量包
矢量包::操作员-(矢量包另一个包){
矢量包;
//检查set1中的项目是否存在于set2中
对于(int i=0;i<(int)items.size();i++){
如果(!anotherBag.包含(项目[i])){
新增(第[i]项);
}
}
返回新包;
}

通过查看您的错误消息,我相信这可能与问题有关 和

基本上,您需要将整个模板代码放在头文件中,而不是单独的.cpp和.h文件。除非显式实例化要在.cpp文件本身中使用的类型(如中所述)


您也没有在cpp文件中包含标题,因为皮特·贝克尔(Pete Becker)第一眼就注意到了您和我的遗漏。他真了不起

编译器应该给你一个问题列表,问题是什么?虽然你可能认为你得到的错误是垃圾,但我们可能可以帮助你破译它们。我不知道如何更正它们。如果编写代码的您不知道如何更正它们,那么intertube上随机出现的陌生人也不太可能会这样做。如果您编写了一堆代码,并且在编写完所有代码之后才尝试编译它,并且被大量错误吓倒,那么问题就在于:您应该编写一小块代码,测试它,确保它工作,然后再编写下一块。不要一次写十页代码,然后坐在那里,看看成百上千的错误,然后试着把它们全部弄清楚。@user1535982,如果我们连错误都看不见,你希望我们如何修复它们?您可能不知道如何更正它们,但我们可能会。以下是一些错误:VectorBag.cpp:7:5:错误:重新定义“int VectorBag::getCurrentSize()const”int VectorBag::getCurrentSize()const{^在VectorBag.h:29:0中包含的文件中,VectorBag.cpp:3:VectorBag.cpp:7:5:注意:'virtual int VectorBag::getCurrentSize()const'之前在此处声明int VectorBag::getCurrentSize()const{^ VectorBag.cpp:12:6:错误:重新定义'bool VectorBag::isEmpty()const'bool VectorBag::isEmpty()'常数{^