Sign in to follow this  
giugio

[c++]copy vector to array

Recommended Posts

I would fill an array of SimpleVertexText* Vvectors wit the content of a vector<SimpleVertexText*> this is the code: SimpleVertexText* Vvectors= new SimpleVertexText[m_pSubMeshData->CountFaceIndexes()]; //SimpleVertexText* vectors = new SimpleVertexText[m_pSubMeshData->CountFaceIndexes()]; SimpleVertexText* V = NULL; for ( int i= 0 ; i <m_pSubMeshData->CountFaceIndexes();i++) { V = new SimpleVertexText(); i_indexes* idx = m_pSubMeshData->GetFaceIndexes(i); tagPositionXYZ* pos = m_pSubMeshData->GetPositionXYZ(idx->nVertex); V->Pos.x = (*pos).x; V->Pos.y = (*pos).y; V->Pos.z = (*pos).z; NORMAL* pnorm = m_pSubMeshData->GetNormal(idx->nNormal); V->Normal.x = (*pnorm).nx; V->Normal.y = (*pnorm).ny; V->Normal.z = (*pnorm).nz; vect.push_back(V); } copy(vect.begin(), vect.end(), Vvectors);//ERROR ERROR ERROR return Vvectors; } the error is at this row: copy(vect.begin(), vect.end(), Vvectors); error message: c:\programmazione\d3dxcreatemeshfvf\emptyproject10\materialphongtexture.cpp(146) : warning C4996: 'std::copy': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2576) : see declaration of 'std::copy' 1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2472) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'SimpleVertexText *' (or there is no acceptable conversion) 1> c:\programmazione\d3dxcreatemeshfvf\emptyproject10\materialphongtexture.h(13): could be 'SimpleVertexText &SimpleVertexText::operator =(const SimpleVertexText &)' 1> while trying to match the argument list '(SimpleVertexText, SimpleVertexText *)' 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2485) : see reference to function template instantiation '_OutIt std::_Copy_opt<_InIt,SimpleVertexText*,std::forward_iterator_tag>(_InIt,_InIt,_OutIt,_InOutItCat,std::_Nonscalar_ptr_iterator_tag,std::_Range_checked_iterator_tag)' being compiled 1> with 1> [ 1> _OutIt=SimpleVertexText *, 1> _InIt=std::_Vector_iterator<SimpleVertexText *,std::allocator<SimpleVertexText *>>, 1> _InOutItCat=std::forward_iterator_tag 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2579) : see reference to function template instantiation '_OutIt std::_Copy_opt<std::_Vector_iterator<_Ty,_Alloc>,_OutIt>(_InIt,_InIt,_OutIt,std::random_access_iterator_tag,std::_Nonscalar_ptr_iterator_tag,std::_Range_checked_iterator_tag)' being compiled 1> with 1> [ 1> _OutIt=SimpleVertexText *, 1> _Ty=SimpleVertexText *, 1> _Alloc=std::allocator<SimpleVertexText *>, 1> _InIt=std::_Vector_iterator<SimpleVertexText *,std::allocator<SimpleVertexText *>> 1> ] 1> c:\programmazione\d3dxcreatemeshfvf\emptyproject10\materialphongtexture.cpp(146) : see reference to function template instantiation 'SimpleVertexText *std::copy<std::_Vector_iterator<_Ty,_Alloc>,SimpleVertexText*>(_InIt,_InIt,_OutIt)' being compiled 1> with 1> [ 1> _Ty=SimpleVertexText *, 1> _Alloc=std::allocator<SimpleVertexText *>, 1> _InIt=std::_Vector_iterator<SimpleVertexText *,std::allocator<SimpleVertexText *>>, 1> _OutIt=SimpleVertexText * 1> ]

Share this post


Link to post
Share on other sites
Your vector contains SimpleVertexText*. You array contains SimpleVertexText. They're two different types - the compiler errors are saying they can't convert SimpleVertexText* into SimpleVertexText.

There doesn't seem to be any real reason why your variable V needs to be dynamically allocated. So you can make your vector a vector<SimpleVertexText> and make V a SimpleVertexText rather than SimpleVertexText*.

Share this post


Link to post
Share on other sites
Your vect-vector seems to hold pointers to SimpleVertexText and not the actual SimpleVertexText's, so copying pointers into an array that holds data won't work. I'm not entirely sure why you use a std::vector at all, you might as well just fill in the data you extract to Vvectors directly in the loop. Like this:


{
SimpleVertexText* Vvectors= new SimpleVertexText[m_pSubMeshData->CountFaceIndexes()];
for ( int i= 0 ; i <m_pSubMeshData->CountFaceIndexes();i++) {
i_indexes* idx = m_pSubMeshData->GetFaceIndexes(i);
tagPositionXYZ* pos = m_pSubMeshData->GetPositionXYZ(idx->nVertex);

Vvectors[i].Pos.x = (*pos).x;
Vvectors[i].Pos.y = (*pos).y;
Vvectors[i].Pos.z = (*pos).z;

NORMAL* pnorm = m_pSubMeshData->GetNormal(idx->nNormal);
Vvectors[i].Normal.x = (*pnorm).nx;
Vvectors[i].Normal.y = (*pnorm).ny;
Vvectors[i].Normal.z = (*pnorm).nz;
}
return Vvectors;
}




However, if you really want to use a std::vector and do your copy-thing, you want to declare the vector as std::vector<SimpleVertexText> vect; and do like this instead.


{
SimpleVertexText* Vvectors= new SimpleVertexText[m_pSubMeshData->CountFaceIndexes()];
for ( int i= 0 ; i <m_pSubMeshData->CountFaceIndexes();i++) {

SimpleVertexText V;
i_indexes* idx = m_pSubMeshData->GetFaceIndexes(i);
tagPositionXYZ* pos = m_pSubMeshData->GetPositionXYZ(idx->nVertex);

V.Pos.x = (*pos).x;
V.Pos.y = (*pos).y;
V.Pos.z = (*pos).z;

NORMAL* pnorm = m_pSubMeshData->GetNormal(idx->nNormal);
V.Normal.x = (*pnorm).nx;
V.Normal.y = (*pnorm).ny;
V.Normal.z = (*pnorm).nz;

vect.push_back(V);
}
copy(vect.begin(), vect.end(), Vvectors);//ERROR ERROR ERROR
return Vvectors;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this