views:

83

answers:

2

So I have a regex expression to parse certain parts of a file name. I'm trying to store each part in its own vector until I use it later, but it won't let me. One error I get when I try making a vector of System::String^ is that error C3698: 'System::String ^' : cannot use this type as argument of 'new' Then, when I try just making a vector of std::string, it can't implicitly convert to type System::String^, and casting won't work either.

void parseData()
{
    System::String^ pattern = "(\\D+)(\\d+)(\\w{1})(\\d+)\\.(\\D{3})";
    std::vector < System::String^ > x, y, filename, separator;

    Regex re( pattern );

    for ( int i = 0; i < openFileDialog1->FileNames->Length; i++ )
    {
        Match^ m = re.Match( openFileDialog1->FileNames[i] );
        filename.push_back( m->Groups[0]->Value );/*
        x.push_back( m->Groups[1]->Value );
        separator.push_back( m->Groups[2]->Value );
        y.push_back( m->Groups[3]->Value );*/
    }                       
}
+1  A: 

std::vector uses new to allocate it's objects, but you can't allocate System::String^ with new, since it's a managed type.

iconiK
Consider using std::string instead, or a managed collection type like List<String^>.
TreDubZedd
@iconiK: I already said that.. @TreDubZedd: I already said i tried std:string as well. I'll try List< System::String^ > tomorrow though.
Justen
@Justen, where did you say that std::vector uses new to allocate it's objects and it can't do that for managed ones?
iconiK
Third sentence.
Justen
But that's not quite the point. You didn't provide an answer, but told me what the error I already posted was (though in slightly more detail). All in all, no new solutions.
Justen
There's no solution if you are intent on using STL collections of managed types. There are workarounds.
user144182
@above - I wasn't intent on using them, just unaware of alternatives. Went with List< String^ > and it worked perfectly.
Justen
+1  A: 

You could use the STL/CLR library if you really prefer using STL collections. It isn't exactly popular, this web page shows you why.

Use List<String^>

Hans Passant