Home > Blockchain >  Type <String^> in list or vector in C /CLI (CLR Console Application)
Type <String^> in list or vector in C /CLI (CLR Console Application)

Time:03-18

About to use a stl::list (or std::vector, previously), I got stuck with this error message:

error C3699: '&&' : cannot use this indirection on type 'System::String ^'

Since I am not very experienced with C /CLI classes interoperability, this error message keeps flashing whenever I declare a std::list or use push_back or emplace_back on it. What is wrong, and how do I use it? Currently, I am studying a book on CLR applications (Microsoft Visual C /CLI by Julian Templeman) Thank you for your help in advance. Code snippet:

void Printout()
{
    String^ strcat(" ");
    String^ path("./somefile.txt");
    FileStream^ inf = gcnew FileStream(path, FileMode::Open, FileAccess::Read);
    list<String^> vect; // = gcnew list<String^>();
    list<String^>::iterator vectit;
    stringstream strst;
    while (strcat != "")
    {
        StreamReader^ sr = gcnew StreamReader(inf, Encoding::UTF8);
        String^ content = sr->ReadToEnd();
        Console::WriteLine(content);
        strcat  = content;
        int preQomma = strcat->IndexOf(",");
        //int postQomma = strcat->IndexOf(",", preQomma   1);
        //Console::WriteLine(strcat->Remove(preQomma, preQomma   1));
        vect.push_back(strcat->Remove(preQomma, preQomma 1)); //bummer
        strcat = "";
    }
    strcat = "";
    for (vectit = vect.begin(); vectit != vect.end();   vectit)
    {
        strcat = strcat   " "   *vectit;
    }
    Console::WriteLine(strcat);
}

CodePudding user response:

An unmanaged class can't contain managed objects, at least not without jumping through hoops that you probably don't want to deal with.

Instead of using std::list or std::vector, use the managed type List<String^>. Since System::Collection::Generic::List is a managed type, it can contain the managed String^ objects.

List<String^>^ mylist = gcnew List<String^>();

mylist->Add(strcat->Remove(preQomma, preQomma 1)); //bummer removed!

That said, a standard warning/reminder: C /CLI is intended for interop, providing a middle layer to allow .Net code to call unmanaged C . While it's certainly possible, you probably don't want to be writing your main application in C /CLI. In general, you probably want to write your application in C#, and use C /CLI for the interop things where P/Invoke isn't sufficient.

  • Related