Jump to content
  • Advertisement
Sign in to follow this  
ELFanatic

how do you pass a std::list object

This topic is 4329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just a quick question. How do you pass a std::list object to a function. I'm new to templates. What I did: I called it with the line foo(theList.begin()); to the function void foo(std::list<System::String> aString) { } It then says "error C2664: 'foo': cannot convert parameter 1 from 'std::list<_Ty>::iterator' to 'aString __gc *' with [ _Ty=System::String ] I'm obviously doing this wrong.

Share this post


Link to post
Share on other sites
Advertisement
foo(theList)

std::list<T>::begin() returns an iterator of type std::list<T>::iterator

If you wished the function to process only a subset of the list you can pass two iterators to it:


typedef std::list<SomeType> TypeList;
typedef TypeList::iterator TypeIterator;
void bar( TypeIterator rangeStart, const TypeIterator &rangeEnd )
{
// process list
}

[edit: im not entirely sure what language you are using. What ive said is for c++, are you using some form of managed c++ or something?]

Share this post


Link to post
Share on other sites
Most likely, you want to pass the list by reference (const or non-const)

if you are going to modify the list, or the objects inside the list, then you should pass it by non-const reference:

void foo (std::list< System::String > & strings)
{
// use it like this:
for (std::list< System::String >::iterator it = strings.begin();
it != strings.end(); ++it)
{
// do something with it
}
}
// ...
// and now the call:
std::list< System::String > myList;
//add stuff to myList
foo(myList);



if you are not going to modify the list or the objects inside it, then you pass it by const reference:

void foo (std::list< System::String > const & strings)
{
// use it like this:
for (std::list< System::String >::const_iterator it = strings.begin();
it != strings.end(); ++it)
{
// do something with it
}
}
// ...
// and now the call:
std::list< System::String > myList;
//add stuff to myList
foo(myList);



so there you have it!

Share this post


Link to post
Share on other sites
that __gc bit in the error looks disturbingly like something from .net and so does the System::String, the problem being that Correct me if im wrong but you cant mix .net types and C++ templates. I could be way off thats just the impression ive always operated under.

Share this post


Link to post
Share on other sites
Quote:
Original post by HippieHunter
that __gc bit in the error looks disturbingly like something from .net and so does the System::String, the problem being that Correct me if im wrong but you cant mix .net types and C++ templates. I could be way off thats just the impression ive always operated under.


I did everything everyone suggested and the compiler is still yelling at me. I think you might be right. That sucks.

Thanks everyone, I think I'm understanding this thing a lot more now.

Now the question is how does one convert a System::String into a std::string?

Share this post


Link to post
Share on other sites
I haven't done this myself, and I didn't consult any tutorials, just the MSDN documentation. But a good bet seems to involve cli::pin_ptr<>. My guess would be something like this:
std::wstring String_cli_to_std(System::String^ s_cli)
{
array<wchar_t>^ char_array = s_cli->ToCharArray();
pin_ptr<wchar_t> pointer = &char_array[0];
return std::wstring(pointer, s_cli->Length);
}

If you need a std::string, instead of std::wstring, you'll have to do the appropriate conversions, but it might be better to just stick with std::wstring if you need to do many conversions. Or maybe just use std::wstring because Unicode is preferable anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Agony
I haven't done this myself, and I didn't consult any tutorials, just the MSDN documentation. But a good bet seems to involve cli::pin_ptr<>. My guess would be something like this:
std::wstring String_cli_to_std(System::String^ s_cli)
{
array<wchar_t>^ char_array = s_cli->ToCharArray();
pin_ptr<wchar_t> pointer = &char_array[0];
return std::wstring(pointer, s_cli->Length);
}

If you need a std::string, instead of std::wstring, you'll have to do the appropriate conversions, but it might be better to just stick with std::wstring if you need to do many conversions. Or maybe just use std::wstring because Unicode is preferable anyway.


as long as std::wstring works with std::list which I imagine it does, then it's cool. wow man, I had done some searches online for this conversion and didn't see anything worthy. Thank you for finding this.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!