简体   繁体   English

无法将参数1从'std :: _ Vector_iterator <_Myvec>'转换为'std :: _ Vector_iterator <_Myvec>'

[英]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- 所以基本上,我要问题 -

  1. why do I get this error? 为什么我会收到此错误? the complex struct is defined in both classes. complex结构在两个类中定义。
  2. where and how do I need to declare on const iterator on class B, so it will be read only? 我在哪里以及如何在B类的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::complexB::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 . AB之外移动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.

相关问题 错误5错误C2039:&#39;update&#39;:不是&#39;std :: _ Vector_iterator &lt;_Myvec&gt;&#39;的成员 - Error 5 error C2039: 'update' : is not a member of 'std::_Vector_iterator<_Myvec>' 错误C2440:“正在初始化”:无法从“ std :: _ Vector_iterator &lt;_Ty,_Alloc&gt;”转换为“类型*” - error C2440: 'initializing' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'type *' 错误C2440:“类型转换”:无法从“ std :: _ Vector_iterator &lt;_Ty,_Alloc&gt;”转换为“ DWORD” - error C2440: 'type cast' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'DWORD' 编译器不能在Vector_const_iterator和Vector_iterator之间“转换”,即使两者的方法都可用 - Compiler can't “convert” between Vector_const_iterator and Vector_iterator, even though methods for both are available 遇到有关_Vector_const_iterator无法转换为_Vector_iterator的错误 - Getting an error about a _Vector_const_iterator not being convertible to a _Vector_iterator 从std :: vector中删除迭代器 - Remove by iterator from std::vector std :: vector迭代器失效 - std::vector iterator invalidation std :: vector的模板化迭代器 - Templated iterator for a std::vector 在std :: vector中找到Iterator - Find Iterator in a std::vector std :: vector迭代器不兼容 - std::vector iterator incompatibles
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM