c++ - 无法使用 vector::插入 "no instance of overloaded function..."

标签 c++ algorithm c++11 vector compiler-errors

class largeNum
{

public:
     std::vector<int>& getValue();

private:
    std::vector<int> value;
};

这是我在 getValue() 方法中使用的基类。

std::vector<int>& largeNum::getValue() {
    return value;
}

尝试在 vector 中插入值时出现错误:

largeNum operator+(largeNum& summand1, largeNum& summand2) {
    largeNum returnNum;
    int size = 0;
    //adapts the smaller vektor to the larger vektor by resizing him and reversing him so 1 turns into 00...01
    if (summand1.getValue().size() > summand2.getValue().size()) {
        for (int i = 0; i < (summand1.getValue().size() - summand2.getValue().size()); i++) {
            //error happens here
            summand2.getValue().insert(0, 0);
        }
    }
[...]

有趣的是,除了 vector::insertvector::erase 之外,我可以使用所有方法。

它给我一个错误,说我需要将两个整数传递给它,我正在这样做。

no instance of overloaded function "std::vector<_Ty, _Alloc>::insert [with _Ty=int, _Alloc=std::allocator<int>]" matches the argument list

最佳答案

没有overloads of vector<T>::insert 就位。它们都带一个迭代器,所以你需要调用 begin在 vector 上得到插入位置。

为了避免调用 summand1.getValue()两次,将结果存储在引用中:

std::vector<int> &tmp = summand1.getValue();
tmp.insert(tmp.begin(), 0);

关于c++ - 无法使用 vector::插入 "no instance of overloaded function...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43437542/

相关文章:

algorithm - FMOD频率分析/归一化

c++ - C++舍入算法中的0.501,C++中的Excel ROUND

c++ - 所有Derived类都必须实现自己的析构函数,否则报编译错误?

c++ - 对自己的互斥锁类使用lock_guard

c++ - Typedef C++ 中的位域/掩码

c++ - 从函数返回一个堆分配的指针好吗?

python - 解析 json 响应以获取父/子字典

c++ - 具有动态值的哈希表

c++ - 映射到方法 c++11

c++ - 如何使用 windows api c++ 处理 "alt"击键