Cannot convert parameter 1 from 'std :: _ Vector_iterator <_Myvec>' to 'std :: _ Vector_iterator <_Myvec>'
I have a question that is a little difficult to describe, so please be calm.
I have two classes, A and B, class A has a private member vector:
class A { private: struct complex { int x; vector< int > y; }; vector< complex > m_resultVector; // <---- the private member public: void getPointerToVector( vector< complex >::iterator it ) { it = m_resultVector.begin(); } };
I need to access (read only) from class B, to this m_resultVector;
, I could write a function get
, but it m_resultVector
's a very long vector and I don't want to copy the whole vector to a new one, I want to send its pointer. also the important part - I need class B which cannot change the contentm_resultVector
class B { struct complex { int x; vector< int > y; }; void functionOf_B() { A class_A; vector< complex >::iterator p_resultVector; class_A.getPointerToVector(p_resultVector); // <------ compilation error here // some reading from the content of p_resultVector } };
when i try to compile it i get an error:
cannot convert parameter 1 from 'std :: _ Vector_iterator <_Myvec>' to 'Std :: _ Vector_iterator <_Myvec>'
so basically, I have questions -
- Why am I getting this error? structure is
complex
defined in both classes. - where and how do I need to declare on
const iterator
in class B so it will be read-only? I'm not sure...
source share
This is because A::complex
and B::complex
are different types (with the same content, but that doesn't matter). So vector<A::complex>
and vector<B::complex>
different. Move the definition struct complex
outside A
and B
.
Also there are more problems in your code. A::getPointerToVector
does nothing as it copies the vector input iterator to the temporary one, assigns a value to it and after returning from this function, all is lost. Using this approach, you need to pass vector<complex>::iterator
as a reference (thus vector<complex>::iterator&
).
I would rather write a method in A
like this
const vector<complex>& get_vector() const { return m_resultVector; }
I am so, you can do it.
void foo() { A class_A; // do something with A const vector<complex>& p_result = class_A.get_vector(); // now you are holding whole vector and you can call methods // defined as const (which does not modify that vector) p_result.begin(); p_result.at(0); p_result.end(); }
source share
Zereges' solution seems to be good. But I understand that you don't want to return a vector. I couldn't find a solution other than below.
In class A:
void getPointerToVector(int position, int &var,vector<int>& o_Vec) { vector<complex>::iterator itr; complex c; c = m_resultVector.at(position); var = c.x; o_Vec = c.y; }
In class B:
void functionOf_B() { A class_A; int aVar; std::vector<int> vec; class_A.getPointerToVector(2, aVar, vec); // some reading from the content of p_resultVector }
I'm not sure how efficient and difficult it is. I would suggest using Zereges' solution better
source share