[RESOLVED] What's wrong with this STL code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] What's wrong with this STL code

  1. #1
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,106

    [RESOLVED] What's wrong with this STL code

    Compiled in 2008, gives error C2275, illegal use of this type as an expression in 2012
    Code:
    template 
    <
    	class tSTLContainer			// The type of STL container to copy.
    >
    void STLCopy (tSTLContainer& outDest, const tSTLContainer& inSource)
    {
    	copy(inSource.begin(), inSource.end(), insert_iterator<tSTLContainer>(outDest, outDest.begin()));
    }
    Never mind. Apparently you need to #include <iterator> now.
    Last edited by GCDEF; June 6th, 2013 at 01:12 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: [RESOLVED] What's wrong with this STL code

    I don't have 2012 in front of me, but maybe you forgot to include <iterator>?

    Here is a full code example
    Code:
    #include <algorithm>
    #include <iterator>
    #include <vector>
    #include <set>
    #include <map>
    
    using namespace std;
    template <class tSTLContainer>
    void STLCopy (tSTLContainer& outDest, const tSTLContainer& inSource)
    {
        std::copy(inSource.begin(), inSource.end(), std::insert_iterator<tSTLContainer>(outDest, outDest.begin()));
    }
    
    int main()
    {
       std::map<int, int> InMap, OutMap;
       STLCopy(OutMap, InMap);
       std::set<int> InSet, OutSet;
       STLCopy(OutSet, InSet);
       std::vector<int> InVect, OutVect;
       STLCopy(OutVect, InVect);
       return 0;
    }
    Regards,

    Paul McKenzie

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,106

    Re: [RESOLVED] What's wrong with this STL code

    Already figured that out, but thanks, you're right.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427

    Re: [RESOLVED] What's wrong with this STL code

    Quote Originally Posted by GCDEF View Post
    Already figured that out, but thanks, you're right.
    The <iterator> header is a real sneaky one in that so many compilers accept code without it being included.

    Regards,

    Paul McKenzie

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,106

    Re: [RESOLVED] What's wrong with this STL code

    Quote Originally Posted by Paul McKenzie View Post
    The <iterator> header is a real sneaky one in that so many compilers accept code without it being included.

    Regards,

    Paul McKenzie
    <functional> is the same way. Not required in 2008 but necessary in 2012. Got a clean build once I figured those two out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center