[英]cannot convert parameter 1 from 'std::_Vector_iterator<_Myvec>' to 'std::_Vector_iterator<_Myvec>'
I have a question, its a bit hard to discribe it so be easy on me please. 我有一个问题,它有点难以描述,所以请对我这么容易。
I have two classes, A and B, class A have a private member- vector: 我有两个类,A和B,A类有一个私有成员 - 矢量:
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 get access (only read) from class B, to this m_resultVector;
我需要从B类访问(仅读取)
m_resultVector;
, I could write a get
function but m_resultVector
is very long vector and I don't want to copy the entire vector to a new one, I want to send its pointer. ,我可以写一个
get
函数,但m_resultVector
是非常长的向量,我不想将整个向量复制到一个新的向量,我想发送它的指针。 also the important part- I need class B cannot change the content of m_resultVector
也是重要的部分 - 我需要B类不能改变
m_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 the error: 当我尝试编译它时,我收到错误:
cannot convert parameter 1 from 'std::_Vector_iterator<_Myvec>' to 'std::_Vector_iterator<_Myvec>'
无法将参数1从'std :: _ Vector_iterator <_Myvec>'转换为'std :: _ Vector_iterator <_Myvec>'
so basically, I have to questions- 所以基本上,我要问题 -
complex
struct is defined in both classes. complex
结构在两个类中定义。 const iterator
on class B, so it will be read only? const iterator
上声明,所以它只是只读? I'm not sure ... That is because A::complex
and B::complex
are different types (with same content, but that does not matter). 这是因为
A::complex
和B::complex
是不同的类型(具有相同的内容,但无关紧要)。 So that vector<A::complex>
and vector<B::complex>
are different. 因此,
vector<A::complex>
和vector<B::complex>
是不同的。 Move definition of struct complex
outside A
and B
. 在
A
和B
之外移动struct complex
定义。
Also there are more issues in your code. 您的代码中还有更多问题。
A::getPointerToVector
does nothing, because it copies input vector iterator to temporary one, assigns a value to it and after return from that function, everything is lost. A::getPointerToVector
什么都不做,因为它将输入向量迭代器复制到临时迭代器,为它赋值,并且从该函数返回后,一切都会丢失。 Using this approach, you would have to pass vector<complex>::iterator
as reference (thus vector<complex>::iterator&
). 使用这种方法,您必须将
vector<complex>::iterator
作为引用传递(因此vector<complex>::iterator&
)。
I would rather write a method in A
like this 我宁愿在
A
写一个方法
const vector<complex>& get_vector() const
{
return m_resultVector;
}
I this way, you can doo this. 我这样,你可以做到这一点。
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();
}
Zereges solution seems good. Zereges解决方案似乎很好。 But I understand you do not want to return vector.
但我明白你不想回归矢量。 I could not come up with any solution other than below.
除了下面,我无法提出任何解决方案。
In Class A: 在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 : 在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 am not sure how efficient and complex this is. 我不确定这是多么有效和复杂。 I would suggest better to use Zereges solution
我建议更好地使用Zereges解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.