简体   繁体   English

如何通过std :: vector <void*> *作为std :: vector <MyStruct*> *功能

[英]How to pass std::vector<void*> * as std::vector<MyStruct*>* to a function

How to pass std::vector<void*> * as std::vector<MyStruct*>* to a function? 如何将std::vector<void*> *作为std::vector<MyStruct*>*传递给函数?

The function declaration is void SetDataReferences(std::vector<MyStruct*>* pVector); 函数声明为void SetDataReferences(std::vector<MyStruct*>* pVector);

if I pass std::vector<void*> * as std::vector<MyStruct*>* I am getting below error 如果我通过std::vector<void*> *作为std::vector<MyStruct*>*我遇到了以下错误

error C2664: 'CTagFilterComboBox::SetDataReferences' : cannot convert parameter 2 from ' std::vector<_Ty> * ' to ' std::vector<_Ty> * ' 错误C2664:'CTagFilterComboBox :: SetDataReferences':无法将参数2从' std::vector<_Ty> * '转换为' std::vector<_Ty> * '

How to resolve this? 如何解决呢?

Unfortunately std::vector<void*> is a completely different type to std::vector<MyStruct*>* , so no cast is going to work. 不幸的是std::vector<void*>是与std::vector<MyStruct*>*完全不同的类型,因此没有std::vector<MyStruct*>*转换。

You need to rebuild the vector from scratch, and pass a pointer to that to your function, and hope that the static_cast on each element is valid. 您需要从头开始重建向量,并将指向该向量的指针传递给您的函数,并希望每个元素上的static_cast有效。

One way, given a std::vector<void*>* foo ; 给定std::vector<void*>* foo

std::vector<MyStruct*> temp;
for (void* p : *foo){
    temp.push_back(static_cast<MyStruct*>(p));
}

If you don't need to modify the vector<void*> foo then you could just work with the underlying data, by changing your function signature to: void SetDataReferences(MyStruct*const* pVector, const size_t length) (remember that const is left associative, so this is a pointer-to-constant-pointers-to-MyStructs.) This can be called as follows: 如果不需要修改vector<void*> foo则可以通过将函数签名更改为以下内容来处理基础数据: void SetDataReferences(MyStruct*const* pVector, const size_t length) (请记住const为左关联,因此这是指向MyStructs的常量指针。)可以这样称为:

SetDataReferences(reinterpret_cast<MyStruct*const*>(data(foo)), size(foo))

If you needed to modify the elements of vector<void*> foo (not the size) you still have the recourse of changing your function signature to: void SetDataReferences(MyStruct** pVector, const size_t length) This can be called as follows: 如果您需要修改vector<void*> foo的元素(而不是大小),您仍然可以将函数签名更改为: void SetDataReferences(MyStruct** pVector, const size_t length)这可以称为:

SetDataReferences(reinterpret_cast<int**>(data(foo)), size(foo))

Live Examples 现场例子

If you need to modify vector<void*> foo that gets more complicated, you'll need to accept the vector<void*> as your function signature does in the question and cast the elements on use rather than as a group. 如果需要修改更复杂的vector<void*> foo ,则需要接受vector<void*>就像函数签名在问题中所做的那样,并在使用时而不是在组中强制转换元素。

I got solution for my problem from this link https://www.codeproject.com/Questions/1261243/How-to-pass-std-vector-void-as-std-vector-mystruct and it is working for me. 我从此链接https://www.codeproject.com/Questions/1261243/How-to-pass-std-vector-void-as-std-vector-mystruct获得了解决问题的方法,它对我有用。

If I call SetDataReferences((std::vector<MyStruct*>*)(&p1)) it is working for me without getting any compiler error. 如果我调用SetDataReferences((std::vector<MyStruct*>*)(&p1))它将为我工作,而不会出现任何编译器错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM