CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy Question about allocate of STL.

    Hi, everyone!

    Here is a sample of the usage of allocate of STL.
    When compiling it with VC6.0, some error occur.

    I have added the source codes and related error
    messages below.

    Source:

    --------
    #include <vector>
    #include <algorithm>
    #include <iostream>
    #include <functional>

    using namespace std;

    int main()
    {
    vector<int> a (2, 5);
    vector<int> b (2, 7);
    int *c = allocate((ptrdiff_t) a.size(), (int*)0 );

    transform ( a.begin(), a.end(), b.begin(),
    raw_storage_iterator<int*, int> (c), plus<int>() );

    copy (&c[0], &c[2], ostream_iterator<int> (cout, " ") );

    return 1;
    }
    --------


    Error messages:

    --------
    C:\Program Files\Microsoft Visual Studio\MyProjects\testRawIterator\testRawIterator.cpp(12) : error C2065: 'allocate' :

    undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\testRawIterator\testRawIterator.cpp(12) : error C2440: 'initializing' :

    cannot convert from 'int' to 'int *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.
    --------


    How to resolve the trouble?


    Thanks in advance,
    George

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    see comments in code below:

    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    #include <functional>
    #include <memory>                   // **** might need on some systems
    
    using namespace std;
    
    int main()
    {
        vector<int> a (2, 5);
        vector<int> b (2, 7);
    
        allocator<int> my_memory;                               // *******
    
        int *c = my_memory.allocate( a.size(), (int*)0 );       // *******
    
        transform ( a.begin(), a.end(), b.begin(),
        raw_storage_iterator<int*, int> (c), plus<int>() );
    
        copy (&c[0], &c[2], ostream_iterator<int> (cout, " ") );
    
    
        my_memory.deallocate(c,a.size());                       // *******
    
        return 1;
    }

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy

    Hi, Philip buddie!

    I have tried your code and it works OK!

    I still have a question, one of the feature of raw_storage_iterator
    is that it can put results directly into uninitialized memory. Can I see this feature in this sample?


    Thanks in advance,
    George

  4. #4
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Talking

    Thanks, Philip buddie!

    George

    Originally posted by Philip Nicoletti
    see comments in code below:

    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    #include <functional>
    #include <memory>                   // **** might need on some systems
    
    using namespace std;
    
    int main()
    {
        vector<int> a (2, 5);
        vector<int> b (2, 7);
    
        allocator<int> my_memory;                               // *******
    
        int *c = my_memory.allocate( a.size(), (int*)0 );       // *******
    
        transform ( a.begin(), a.end(), b.begin(),
        raw_storage_iterator<int*, int> (c), plus<int>() );
    
        copy (&c[0], &c[2], ostream_iterator<int> (cout, " ") );
    
    
        my_memory.deallocate(c,a.size());                       // *******
    
        return 1;
    }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured