Reputation: 143
class largeNum
{
public:
std::vector<int>& getValue();
private:
std::vector<int> value;
};
This is the base class I'm using with the getValue() method.
std::vector<int>& largeNum::getValue() {
return value;
}
The error now appears trying to insert values in 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 apart from vector::insert
and vector::erase
.
It gives me an error saying I would need to pass it two ints, which I am doing.
no instance of overloaded function "std::vector<_Ty, _Alloc>::insert [with _Ty=int, _Alloc=std::allocator<int>]" matches the argument list
Upvotes: 4
Views: 8679
Reputation: 7447
Your first parameter to insert()
must be an iterator, not an int
.
Furthermore, having a public member function returning a reference to a private member is a bad smell since you are in fact breaking encapsulation.
Thus my suggestion is to use the pattern "tell, don't ask":
class largeNum
{
public:
void insertFront(int value); // implemented via value.insert() and value.begin()
private:
std::vector<int> value;
};
Upvotes: 2
Reputation: 726479
None of the overloads of vector<T>::insert
takes position. They all take an iterator, so you need to call begin
on the vector to get the insertion position.
To avoid calling summand1.getValue()
twice, store the result in a reference:
std::vector<int> &tmp = summand1.getValue();
tmp.insert(tmp.begin(), 0);
Upvotes: 5