1

In the SNAP Library there's a method that allows me to save a file in my pc here it is:

TSnap::SaveEdgeList(G, q, "Edge list format");`

In this function the 2nd argument its type is TStr which represents string types in SNAP library

I have a string^ variable that contains a full directory of where I want to put my file like this (P.S: it contains only one backslash so i have to replace it with double backslash)

string^ filename = openFileDialog1->FileName;

Then i convert filename variable of type string^ to std::string like this:

std::string s = filename->ToString;

What I want to do is to give the content of the string variable s to a TStr variable and with the help of of fellow members here i did like this:

TStr q = s.c_str();;

But unfortunately it still gives an error:

error C3867: 'System::String::ToString': non-standard syntax; use '&' to create a pointer to member

Does anyone have a suggestion, or an alternative solution, or something?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
hamza saber
  • 511
  • 1
  • 4
  • 18
  • 1
    The error is most likely related to `std::string s = filename->ToString;` which should be `std::string s = filename->ToString();` – Killzone Kid May 02 '18 at 21:04
  • nope when i do that it gives an error caus ToString without () is a Function of the type string^ not string (there's a difference) – hamza saber May 02 '18 at 21:08
  • No, the error is [EXACTLY](https://imgur.com/a/3Deauj8) because of that – Killzone Kid May 02 '18 at 21:17
  • @KillzoneKid well here 's the error that gives me: `1>c:\users\morganis\source\repos\project4\project4\myform.h(201): error C2440: 'initializing': cannot convert from 'System::String ^' to 'std::basic_string,std::allocator>' 1>c:\users\morganis\source\repos\project4\project4\myform.h(201): note: No constructor could take the source type, or constructor overload resolution was ambiguous` – hamza saber May 02 '18 at 21:24

1 Answers1

1

It looks like you are using System Strings component extension. In this case you can use marshalling for conversion from System::String ^ to std::string. Include: <msclr/marshal_cppstd.h> and use msclr::interop::marshal_as method, like this:

#include <iostream>
#include <string>
#include <msclr/marshal_cppstd.h>

class Dialog
{
public:
    System::String^ FileName()
    {
        return "name";
    }
};

int main(array<System::String ^> ^args)
{
    Dialog *openFileDialog1 = new Dialog;
    System::String^ filename = openFileDialog1->FileName();
    std::string s = msclr::interop::marshal_as<std::string>(filename->ToString());

    std::cout << s << std::endl;

    return 0;
}
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37