PRB: C2664 Instantiating STL List with Sequence [first, last)Last reviewed: July 24, 1997Article ID: Q168404 |
The information in this article applies to:
SYMPTOMSInstantiating an STL list container with a sequence [first, last), where first and last are iterators, causes the compiler error C2664:
'function': cannot convert parameter number from 'type1' to 'type2' CAUSEUsing the list(const_iterator first, const_iterator last, const A& a1= A()) constructor causes the compiler error C2664. The constructor for list takes a list<T>::const_iterator instead of a generic iterator for the first two parameters.
RESOLUTION
Workaround 1Use the default constructor. Insert elements in the list using the push_back member function.
Workaround 2Use the list(size_type, const T& v = T(), const A& a1 = A()) constructor. Insert elements in the list using the copy algorithm.
STATUSMicrosoft is researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.
MORE INFORMATIONThe following sample demonstrates the problem:
//test1.cpp //Compiler options needed: /GX #include <list> int main() { int INTARRAY[4] = {1, 2, 3, 4} ; std::list<int> INTLIST(INTARRAY, INTARRAY + 4) ; //C2664 here }The exact error message for the above sample is:
test1.cpp(9) : error C2664: 'std::list<int,class std::allocator<int>>::list<int,class std::allocator<int>>(unsigned int,const int &,const class std::allocator<int> &)' : cannot convert parameter 1 from 'int [4]' to 'unsigned int'The following sample demonstrates Workaround 1:
//test2.cpp //Compiler options needed: /GX #include <list> int main() { int INTARRAY[4] = {1, 2, 3, 4} ; std::list<int> INTLIST ; for(int i = 0; i < 4; i++) INTLIST.push_back(INTARRAY[i]) ; return 0 ; }The following sample demonstrates Workaround 2:
//test3.cpp //Compiler options needed: /GX #include <list> #include <algorithm> int main() { int INTARRAY[4] = {1, 2, 3, 4} ; std::list<int> INTLIST(4) ; std::copy(INTARRAY, INTARRAY + 4, INTLIST.begin()) ; return 0 ; } |
Additional query words: list copy push_back
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |