I am so frustrated right now after several hours trying to find where shared_ptr is located. None of the examples I see show complete code to include the headers for shared_ptr (and working). Simply stating std, tr1 and <memory> is not helping at all! I have downloaded boosts and all but still it doesn't show up! Can someone help me by telling exactly where to find it?
Thanks for letting me vent my frustrations!
EDIT:
I see my title has been changed. Sorry about that.
So... it was also because it was not clear to me that shared_ptr is "C++ version dependant" --> that's why I did not state my environment --> therefore probably why it was so difficult for me to find it.
I am working on MSVS2008.
EDIT 2:
I don't know why, but I was including [memory] and [boost/tr1/memory.hpp] and [boost/tr1/tr1/memory] while looking everywhere for the shared_ptr.. of course, i couldn't.
Thanks for all the responses.
There are at least three places where you may find shared_ptr:
If your C++ implementation supports C++11 (or at least the C++11 shared_ptr), then std::shared_ptr will be defined in <memory>.
If your C++ implementation supports the C++ TR1 library extensions, then std::tr1::shared_ptr will likely be in <memory> (Microsoft Visual C++) or <tr1/memory> (g++'s libstdc++). Boost also provides a TR1 implementation that you can use.
Otherwise, you can obtain the Boost libraries and use boost::shared_ptr, which can be found in <boost/shared_ptr.hpp>.
Boost Getting Started
If you want to use it from Boost TR1 instead
shared_ptr Example
for VS2008 with feature pack update, shared_ptr can be found under namespace std::tr1.
std::tr1::shared_ptr<int> MyIntSmartPtr = new int;
of
if you had boost installation path (for example # C:\Program Files\Boost\boost_1_40_0) added to your IDE settings:
#include <boost/shared_ptr.hpp>
If your'e looking bor boost's shared_ptr, you could have easily found the answer by googling shared_ptr, following the links to the docs, and pulling up a complete working example such as this.
In any case, here is a minimalistic complete working example for you which I just hacked up:
#include <boost/shared_ptr.hpp>
struct MyGizmo
{
int n_;
};
int main()
{
boost::shared_ptr<MyGizmo> p(new MyGizmo);
return 0;
}
In order for the #include to find the header, the libraries obviously need to be in the search path. In MSVC, you set this in Project Settings>Configuration Properties>C/C++>Additional Include Directories. In my case, this is set to C:\Program Files (x86)\boost\boost_1_42
Related
I am doing some arduino development using cpp and h files and I am having some troubles using string with them. Currently I have
#include <string>
at the top of both the cpp and the h file. When I do that it gives me the error:
string: no such file or directory
If I go into the h file and change it to
#include <string.h>
then it gives me the error:
std::string has not been declared
Anytime I use the string I use: std::string to declare it. I am not using namespace std and these files were working together fine before I started to try to use string. I am new to C/C++ so I appreciate any help. Thanks!
In short, there is a way to use std::string with the Arduino.
TL;DR:
link to the arduino STLv1.1.2
NOTE
Please note that currently the harrdwareserialstream class provided by this STL should be considered broken (as per my testing, with version 1.6.5 of the IDE, and possibly anything after 1.0.6). therefore, you can't use
hardwareserialstream << "Hi there person number " << (int)i
and so on. it seems to no longer work due to taking a reference to the serial port it would interact with rather than a pointer - in short, continue using
Serial.print("Hi there person number");
Serial.print((int)i);
Lastly the serial classes don't know what a std::string is, so if using them, give it std::string.c_str() instead
Background
As McEricSir says in the comments, the arduino does provide its own string class, though i have found it to have problems related to memory leakage, which eventually ate all of the memory i had and the program stopped running - though this was in the arduino IDE v 1.0.5, it may have been fixed since then.
I had the same problem, and found someone who had created a version of the STL for the arduino (props to Andy Brown for this) which is a cutdown version of the SGI STL. it provides std::string, std::vector and a large amount of the STL to the arduino.
there are some things to be aware when using it though; if you have a board with very little memory, you will fill it quite quickly using the smart containers and other advanced features.
Using the Library
To use the library, you'll need to read the article, though I'll summarise the main points for you here:
Installation
simply extract the library to (assuming you are using the standard Arduino IDE) hardware\tools\avr\avr\include folder and you are good to go.
Using It
To actually use the new libraries, you need to include 2 additional things as well as the library you wanted.
firstly, you need to include the header iterator BEFORE any libraries that come from this STL - and in every file you reference the STL in.
Secondly, you also need to include the file pnew.cpp to provide an implementation of the new operator for the STL to work with.
Lastly, include any header files as you would normally.
to make use of the types gained from them, don't forget the the std:: namespace notation for them. (std::string and so on)
Bugs with it
Since Andy posted the library, there have been two bugs (that i'm aware of).
The first one Andy himself rectifies and explain in the blog post:
The compiler will spit out a typically cryptic succession of template errors, with the key error being this one:
dependent-name std::basic_string::size_type is parsed as a non-type,
but instantiation yields a type c:/program files (x86)/arduino-1.0/
hardware/tools/avr/lib/gcc/../../avr/include/string:1106: note:
say typename std::basic_string::size_type if a type is meant
Basically the STL was written a long time ago when C++ compilers were a little more forgiving around dependent types inherited from templates. These days they are rightly more strict and you are forced to explicitly say that you mean a type using the typename keyword.
Additionally, he provides the updated version for you to grab.
Lastly, there are reports in the comments about a bug in the newer versions of the IDE pertaining to the vector class where the compiler complains about the use of _M_deallocate without a prepending this->, which you can fix if you search for them inside the vector class
For your convenience
As i use this quite frequently, i've packaged up the current version, which can be found here (this includes both the fixes i have commented on)
Lastly
When using this, make sure to keep an eye on your free memory, and to that end i recommend the excellent class MemoryFree Library found here
on a side note if you #include<string> inside the header you won't need to include it in the relevant .cpp file
I've been experiencing this error for a while now, on every project that I have built with the CryEngine2 SDK in Visual Studio 2013 Professional.
Most of the time, I have just edited the function from this:
void CMultipleGrabHandler::Reset()
{
std::for_each (m_handlers.begin(), m_handlers.end(), std::mem_fun (&CBaseGrabHandler::Reset));
std::vector <CAnimatedGrabHandler*>::iterator it = m_handlers.begin();
std::vector <CAnimatedGrabHandler*>::iterator end = m_handlers.end();
for ( ; it != end; ++it)
delete *it;
m_handlers.erase (m_handlers.begin (), m_handlers.end ());
}
to this:
void CMultipleGrabHandler::Reset()
{
}
I know it's not a good approach to the problem, but it got rid of the 'mem_fun' : is not a member of 'std' error.
I am now looking for a solution to overcome this problem, since I have just started working on a new project, where stability is key; it's not good if I remove the body of CMultipleGrabHandler::Reset(), since it could possibly induce crashes in certain situations.
I have browsed the internet for a solution to this problem, but I have not found anything (closest thing I found was an explanation of what mem_fun does).
I have tried taking std:: off mem_fun, but I just get an mem_fun is undefined error, suggesting that this isn't exactly the right way to go about mending this error.
The project was originally created in Visual Studio 2005, and was migrated to Visual Studio 2013's format when I first opened the solution.
Might this be the cause of the problem?
How can it be fixed?
Edit: Added visual-studio-2015 as this also applies to the new VS version.
The function std::mem_fun is defined in the header <functional>.
On some older compilers, you don't necessarily need to #include a standard library header in order to use functions and classes defined in that header, because it might already be included by some other standard library header that you #include. This is not exclusive to VS; it was also true for older gcc versions, like 4.0. Newer compilers are more standard-conforming and will require you to #include the actual headers that define standard library functions and classes.
I can't get my following c++ program compiled in Visual Studio 2010. I already have a working build of the same code so I know the code is correct. But I have no idea with what or how it was compiled.
So I would appreciate if someone could just copy the code and try to compile it in VS 2010.
Code:
http://codepad.org/4VtrVBdK
new:
Ok, I did editing according to the comments below. Now the only problems that seem to have remained are related to calls to the overloaded functions. So how to go about it?
so I know the code is correct
What you "know" is false. The code is wrong in many ways. Here is but one example:
for(unsigned int i=0;i<GPNO;i++) //SORTING ACCORDING TO FITNESS
for(unsigned int j=i+1;j<GPNO;j++)
if((gp[i]->fitness)>(gp[j]->fitness))
{
gp[i]->mycopy(tmp);
gp[j]->mycopy(gp[i]);
tmp->mycopy(gp[j]);
}
for(i=1;i<=no;i++)
{
gp[i]->mycopy(gp[GPNO-i]);
}
In the second for loop, i is undeclared. I suspect the original compiler was VC6, which allowed this.
Another problem is the way you're calling pow. You call it with macros (which are patently evil for this purpose), for instance:
pf[i].frq+=(unsigned int)pow(2,2*PF-1);
And the compiler doesn't know which version of pow you had in mind. Case in point for macros being evil for this purpose. Do this:
pf[i].frq+=(unsigned int)pow(2.0,2*PF-1);
Or better yet, get rid of the macros.
Another example of your code being wrong:
#include "stdlib.h"
#include "conio.h"
#include "math.h"
None of these includes are part of the Standard. If you can get them to compile, its only because your compiler was anticipating your mistake. But it's still a mistake.
Looks like you're missing using namespace std;
I'm considering using boost::ptr_container as a result of the responses from this question. My biggest problem with the library is that I cannot view the contents of the collection in the debugger, because the MSVC debugger doesn't recognize it, and therefore I cannot see the contents of the containers. (All the data gets stored as void * internally)
I've heard MSVC has a feature called "debugger visualizers" which would allow the user to make the debugger smarter about these kinds of things, but I've never written anything like this, and I'm not hugely firmiliar with such things.
For example, compare the behavior of boost::shared_ptr with MSVC's own std::tr1::shared_ptr. In the debugger (i.e. in the Watch window), the boost version shows up as a big mess of internal variables used for implementing the shared pointer, but the MSVC version shows up as a plain pointer to the object (and the shared_ptr's innards are hidden).
How can I get started either using or implementing such a thing?
See this link which provides every debugger visualizer (through autoexp.dat) you may want :
All visualizers are available in the
svn. Currently, we support the following
Boost types:
boost::array, ptr_array, ptr_deque, ptr_list, ptr_map,
ptr_multimap, ptr_set,
ptr_multiset, ptr_vector
boost::interprocess::offset_ptr
boost::optional
boost::multi_index_container
boost::shared_ptr
boost::posix_time::ptime,
boost::posix_time::time_duration (two variants are available)
boost::regex
boost::variant
Some possibly useful information on MSDN:
for VC++ 8
for VC++ 10
A codeproject sample or two:
http://www.codeproject.com/Articles/51610/Visualizing-MFC-Containers-in-autoexp-dat.aspx
http://code.msdn.microsoft.com/boostsharedptrvis
All of them involve autoexp.dat in some way, making that an effective search term.
You can use this extension for Visual Studio 2012+, check this link. They based on visualizers from boost svn for Visual Studio 2008/2010
Extension support the following Boost types:
boost::shared_ptr, boost::weak_ptr, boost::intrusive_ptr, boost::shared_array, boost::scoped_ptr, boost::scoped_array
boost::ptr_array, boost::ptr_vector, boost::ptr_list, boost::ptr_deque, boost::ptr_map, boost::ptr_set, boost::ptr_multimap, boost::ptr_multiset
boost::array, boost::dynamic_bitset, boost::circular_buffer
boost::unordered_map, boost::unordered_set, boost::unordered_multimap, boost::unordered_multiset
boost::intrusive::list, boost::intrusive::slist
boost::container::basic_string, boost::container::deque, boost::container::vector
boost::optional, boost::any, boost::variant
boost::filesystem::path, boost::filesystem::directory_entry, boost::filesystem::file_status
boost::posix_time::ptime, boost::posix_time::time_duration
boost::regex
boost::interprocess::offset_ptr
boost::tribool
boost::unique_lock
boost::uuids::uuid
Debugger visualizers are only available for managed code, according to http://msdn.microsoft.com/en-us/library/zayyhzts.aspx which provides more information about them.
I am following the quickstart guide for boost::spirit, and I get this compiler warning when I include : "This header is deprecated. Please use: boost/spirit/include/classic_core.hpp" Should I be worried about this?
(quick start guide: http://spirit.sourceforge.net/distrib/spirit_1_8_5/libs/spirit/doc/quick_start.html , with full source of the program I am trying to compile here: http://spirit.sourceforge.net/distrib/spirit_1_8_5/libs/spirit/example/fundamental/number_list.cpp)
edit: Additionally, when I try to compile with the recommended classic_core.hpp and classic_push_back_actor.hpp headers, I get the following compiler errors:
test7.cpp: In function 'bool parse_numbers(const char*, __gnu_debug_def::vector<double, std::allocator<double> >&)':
test7.cpp:18: error: 'real_p' was not declared in this scope
test7.cpp:18: error: 'push_back_a' was not declared in this scope
test7.cpp:23: error: 'space_p' was not declared in this scope
test7.cpp:23: error: 'parse' was not declared in this scope
[EDIT:] The original answer is badly out of date; in particular the link is broken. The current version of Boost (since 2012-02-24) is 1.49.0.
The warning mentioned is a result of #include <boost/spirit.hpp> which is a deprecated header; however old examples on the web use this form. To get started, try the boost tutorials. Once you see the correct includes and namespaces, most old examples can easily be converted.
[OLD ANSWER:]
You must be using boost 1.39 or later (via SVN). This presentation should help:
http://www.boostcon.com/site-media/var/sphene/sphwiki/attachment/2009/05/07/SpiritV2.pdf
In short, there's a brand new way of doing thing and these are the namespaces to use:
boost::spirit:qi (for the parser)
boost::spirit::karma (for the generator lib)
The official release is 1.40 so probably by that time the doc will be updated.
EDIT: the doc in the boost SVN repository is being worked on and probably reflect the new architecture in a more faithful manner.
When you're including the classic headers the parsers are in the boost::spirit::classic namespace. Try:
using namespace boost::spirit::classic;
When a library indicates that a class/header/method/etc. is deprecated, it means that the maintainer of the library will most likely stop maintaining the functionality, and may remove it in the future. I would recommend to switch to the suggested header sooner than later, so save yourself from headaches in the future.
The new header may have a slightly different way of handling the feature, so you may need to make some code changes.
(I don't know much about boost, this is just a general comment)