typedef struct MyStruct {
unordered_set<int> mySet;
}MyStruct;
int main(){
unordered_set<int> sset{ 1,2,3,4,5 };
MyStruct tk;
tk.mySet = sset; //This works fine.
for (auto x : tk.mySet) printf("%d ", x);
MyStruct *Arr = (MyStruct *)malloc(10* sizeof(MyStruct));
Arr[0] = tk; // This isnt working, giving an exception
//the same code runs as expected on codeblocks
for (auto x : Arr[0].mySet) printf("%d ", x);
return 0;
}
C'est l'exception que je reçois lorsque j'exécute le débogueur.Exception unordered_set de Visual Studio, fonctionne sur les blocs de code
<fileName>.exe!std::list<int,std::allocator<int> >::_Orphan_ptr(std::_List_node<int,void *> * _Ptr) Line 1919 C++ <fileName>.exe!std::list<int,std::allocator<int> >::clear() Line 1501 C++ <fileName>.exe!std::_Hash<std::_Uset_traits<int,std::_Uhash_compare<int,std::hash<int>,std::equal_to<int> >,std::allocator<int>,0> >::_Copy(const std::_Hash<std::_Uset_traits<int,std::_Uhash_compare<int,std::hash<int>,std::equal_to<int> >,std::allocator<int>,0> > & _Right) Line 843 C++ <fileName>.exe!std::_Hash<std::_Uset_traits<int,std::_Uhash_compare<int,std::hash<int>,std::equal_to<int> >,std::allocator<int>,0> >::operator=(const std::_Hash<std::_Uset_traits<int,std::_Uhash_compare<int,std::hash<int>,std::equal_to<int> >,std::allocator<int>,0> > & _Right) Line 291 C++ <fileName>.exe!std::unordered_set<int,std::hash<int>,std::equal_to<int>,std::allocator<int> >::operator=(const std::unordered_set<int,std::hash<int>,std::equal_to<int>,std::allocator<int> > & _Right) Line 187 C++ <fileName>.exe!main() [External Code] [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
Où avez-vous lu que l'utilisation de 'malloc' pour créer des types de classes complexes en C++ serait acceptable? Nous devons corriger la ressource d'apprentissage que vous utilisiez. –
Je ne savais pas que jouer juste autour, btw pourquoi cela a-t-il fonctionné sur gcc? – praveen
Ce n'est pas le cas. Il est simplement apparu, par pur hasard. –