Jump to content
  • Advertisement
Sign in to follow this  
giugio

[c++]copy vector to array

This topic is 3401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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.Pos.x = (*pos).x;
Vvectors.Pos.y = (*pos).y;
Vvectors.Pos.z = (*pos).z;

NORMAL* pnorm = m_pSubMeshData->GetNormal(idx->nNormal);
Vvectors.Normal.x = (*pnorm).nx;
Vvectors.Normal.y = (*pnorm).ny;
Vvectors.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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!