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::insert
和 vector::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/