Public Group

# 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.

## 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 on other sites
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 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 myListfoo(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 myListfoo(myList);

so there you have it!

##### 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 on other sites
Quote:
 Original post by HippieHunterthat __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 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 on other sites
Quote:
 Original post by AgonyI 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^ char_array = s_cli->ToCharArray(); pin_ptr 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.

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632665
• Total Posts
3007709
• ### Who's Online (See full list)

There are no registered users currently online

×