& getValue(); private: std::ve...">

Cannot use vector :: insert "no instance of overloaded function ..."

class largeNum
{

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

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

      

This is the base class that I am using with the getValue () method.

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

      

An error appears trying to insert values ​​into vectors:

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);
        }
    }
[...]

      

Interestingly, I can use all methods besides vector::insert

and vector::erase

.

It gives me an error saying that I need to pass two ints to it, which I am doing.

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

+3


source to share


2 answers


None of the G-forces arevector<T>::insert

in position. They all take on an iterator, so you need to call begin

on a vector to get the insertion position.

To avoid being called summand1.getValue()

twice, store the result in a link:



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

      

+3


source


Your first parameter insert()

should be an iterator, not int

.

Also, having a public member function returning a private member reference is a bad smell since you are actually breaking encapsulation.



So my suggestion is to use the "tell, not ask" pattern:

class largeNum
{

public:
     void insertFront(int value);  // implemented via value.insert() and value.begin()

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

      

+1


source







All Articles