C++ 没有匹配的函数来调用 vector 中的删除

标签 c++ function c++11 vector erase

我花了几个小时研究并试图找出这个错误,但没有成功。给定要删除的索引,我无法从 vector 中删除。删除给定索引的函数位于 tuple.cpp 中,如 tuple.h 中所述。

元组.h:

#pragma once
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Tuple {
    private:
        vector<string> tuVec;
    public:
        Tuple() {};
        string toString() const;
        void eraseFromVec(int index) const;
        bool operator< (const Tuple &right) const {
            string temp = toString();
            string temp2 = right.toString();
            return temp < temp2;
        }
};

元组.cpp:

#include "tuple.h"

void Tuple::eraseFromVec(int index) const {
    tuVec.erase(tuVec.begin() + index);
    return;
}

每个 Tuple 对象都是集合的成员,集合是 Relation 类的成员。

关系.h:

#pragma once

#include "tuple.h"
#include <iostream>
#include <set>
#include <string>
#include <sstream>
#include <iterator>

using namespace std;

class Relation {
    private:
        string name;
        set<Tuple> tuples;
    public:
        Relation(string rName, Scheme rScheme);
        void addTuple(Tuple newTuple);
};

这是调用删除函数的代码:

set<Tuple>::iterator it;
    for(it = tuples.begin(); it != tuples.end(); ++it) {
        int size = it->tuVec.size() - 1;
        int i;
        for(i = size; i > -1; --i) {
            bool isIn = false;
            int j;
            int size2 = tempRel.toKeep.size();
            for(j = 0; j < size2; ++j) {
                if(//condition) {
                    isIn = true;
                }
            }
            if(!isIn) {
                it->eraseFromVec(i);
            }
        }
    }

这是我收到的错误消息:(我尝试在此处输入它,但由于某种原因它不起作用,所以我附上了它的图片)

My error message

我希望以前没有人问过这个问题。我已经在互联网上搜索了几个小时试图找到解决方案,但一直无法找到。如果有任何帮助,我将不胜感激。

最佳答案

您的问题似乎是您的函数 eraseFromVec 有一个 const 限定符。但它调用了 std::vectorerase 函数,该函数是非常量的。只需删除 const 限定符,它就应该可以正常工作。

关于C++ 没有匹配的函数来调用 vector 中的删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090189/

相关文章:

c++ - “字符串”未命名类型-C++错误

c++ - 如何检测一个类是否有移动构造函数?

c++ - 尝试对数组进行排序

c++ - asio :how does server actively send information to client while listening to client at the same time?

c++ - 比较 C++ 中的 tm 类型

javascript - Promise 和 Promise.all(array) 在数组完成之前执行

c++ - 明确定义的移位超过 32 位,没有编译器警告

C++函数返回值未赋值

javascript - 如何分离两个点击功能?

c++ - Boost.Regex 与 C++11 正则表达式