-
September 30th, 2011, 08:57 PM
#1
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.
-
September 30th, 2011, 09:19 PM
#2
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
-
September 30th, 2011, 11:58 PM
#3
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
-
October 1st, 2011, 07:48 AM
#4
Re: Vector of pointers
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
-
October 1st, 2011, 11:27 AM
#5
Re: Vector of pointers
Originally Posted by nuzzle
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...
-
October 1st, 2011, 01:18 PM
#6
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
-
October 1st, 2011, 02:13 PM
#7
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.
-
October 1st, 2011, 02:18 PM
#8
Re: Vector of pointers
Originally Posted by geoconker
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|