CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2011
    Posts
    9

    Vector of pointers

    Hello,

    I'm a newbie at C++, therefore some concepts of STL and the boost lib in general are still a mystery to me. I'm trying to use a global vector of pointers but my compiler won't let me:

    Code:
        //global stuff
        typedef boost::scoped_ptr<MyClass> P_MyClass;
        std::vector<P_MyClass> globalMyVec;
    
        int main (void) {
    
            //some code...
            
            globalMyVec.push_back(new MyClass(arg)); 
    
            //more code, this time related to threads...
        }
    the error is:

    src/Main.cpp:55:43: error: no matching function for call to 'std::vector<boost::scoped_ptr<MyClass> >:ush_back(MyClass*)'
    c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_vector.h:741:7: note: candidate is: void std::vector<_Tp, _Alloc>:ush_back(const value_type&) [with _Tp = boost::scoped_ptr<MyClass>, _Alloc = std::allocator<boost::scoped_ptr<MyClass> >, value_type = boost::scoped_ptr<MyClass>]


    It's telling me to use an std::allocator, but why? If I change globalMyVec from type boost::scoped_ptr<P_MyClass> to type boost:scoped_ptr<MyClass *>, this error seems to go away and the thread errors start popping up (still figuring out how to use boost:thread in Windows, I'm getting some "undefined reference" errors). So to sum it up, I have 2 questions:

    1- Why did the change from P_MyClass to MyClass * made the error go away? I thought boost:scoped_ptr<MyClass> and MyClass * were treated the same way in this case...
    2- How come the compiler ignored my thread errors before I made this change? Why didn't it show them since the very beginning if they existed?

    Thanks in advance.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Vector of pointers

    1- http://www.boost.org/doc/libs/1_47_0...scoped_ptr.htm
    2- Can't say since we haven't see the code or the errors.

    gg

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Vector of pointers

    You can try this,

    globalMyVec.push_back(P_MyClass(new MyClass(arg)));

    or in two steps,

    P_MyClass smartp(new MyClass(arg)); // instantiate object and assign to smart pointer
    globalMyVec.push_back(smartp); // store smart pointer in vector

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Vector of pointers

    Quote Originally Posted by 1- http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/scoped_ptr.htm

    scoped_ptr cannot be used in C++ Standard Library containers.
    gg

  5. #5
    Join Date
    May 2011
    Posts
    9

    Re: Vector of pointers

    Quote Originally Posted by nuzzle View Post
    You can try this,

    globalMyVec.push_back(P_MyClass(new MyClass(arg)));

    or in two steps,

    P_MyClass smartp(new MyClass(arg)); // instantiate object and assign to smart pointer
    globalMyVec.push_back(smartp); // store smart pointer in vector

    That and changing from scoped_ptr to shared_ptr did the job, thank you.

    The thread erros are all like this one:

    ../boost_1_47_0/boost/thread/detail/thread.hpp:252: undefined reference to `_imp___ZN5boost6thread12start_threadEv'

    It seems I need to build the boost::thread class for Windows, correct? What I don't understand is why my compiler didn't warn me about these errors before I fixed the previous error, shouldn't it display all errors it can find every time I ask it to compile a project for me? Makes no sense to me...

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Vector of pointers

    That's a linker error. You have to link to get linker errors. You can't link until you compile successfully.

    boost::thread has to be compiled - it's not a "header only" boost library.

    gg

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Vector of pointers

    Compiling boost isn't too hard, though. You pretty much just open the bootstrap.bat file and then, after it has created the bjam executable, run that. You can pass options to bjam if you like but that may not be necessary.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Vector of pointers

    Quote Originally Posted by geoconker View Post
    What I don't understand is why my compiler didn't warn me about these errors before I fixed the previous error, shouldn't it display all errors it can find every time I ask it to compile a project for me? Makes no sense to me...
    Aside from the distinction between compiler errors and linker errors which has been mentioned, there's an even earlier stage: preprocessor errors. This could be things like #includes of unknown files.

    Within a single stage, the IDE will report all the errors it can see. However, you should not assume this is a real representation of all errors. First, a single error can confuse the compiler enough that it gives up and doesn't continue to parse a particular portion of the code, leaving later errors undetected. Second, a single error (say, in a typedef) can result in numerous error messages (say, wherever that typedef is used).

    Therefore, the best strategy is to fix at most only the first 2-3 errors reported before attempting another compile.

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