I have the following code.
#include <iostream>
#include <vector>
template <typename T> class testTemp {
std::vector<T> container;
public:
std::vector<T&> getTest(int nrOfEle);
void push(T row){container.push_back(row);}
};
template<typename T>
std::vector<T&> testTemp<T>::getTest(int nrOfEle) {
std::vector<T&> refContainer;
for(int i =0; i < nrOfEle; i++){
refContainer.push_back(container[i]);
}
return refContainer;
}
int main() {
testTemp<std::vector<int> > test; // T =std::vector<std::int>
std::vector<int> fillMeUp;
for(int i=0; i < 10; i++){
fillMeUp.push_back(i);
}
test.push(fillMeUp);
std::vector<int> container = test.getTest(3); // when i change the elements in container
container[0] = 999; //this change should also affect the private `member inside the testTemp class`
return 0;
}
What I want to be able to do, is from main call the function getTest() with the number of elements I want to change. In the code above I want to change the 3 first elements. So I input that into the function. The function should then return a container to main containing the 3 first elements of the private member std::vector container as references so that when i make a change in main it also changes the element in the class container std::vector container. I'm not quite sure how to achieve this. The code above is my first attempt but it results in an error
"/usr/include/c++/9/ext/new_allocator.h:63:26: error: forming pointer
to reference type ‘std::vector<int>&’ 63 | typedef _Tp*
pointer;"
after reading the comment by formerlyknownas_463035818 i've changed to
template<typename T>
T* testTemp<T>::getTest(int nrOfEle) {
return &container[nrOfEle];
}
however this seems to return the address of the vector object rather than the element