System::String for basic_string

Asked

Viewed 74 times

0

I was looking for how to convert System::String to "normal" string, and found a command:

#include <iostream>
#include <string>
#include <msclr\marshal_cppstd.h>
System::String^ mstr = "PTSO";
std::string mstrr msclr::interop::marshal_as< string >(mstr);

It worked, but I tried to make a function to "facilitate" the use, and I could not. I tried so:

#include <iostream>
#include <string>
#include <msclr\marshal_cppstd.h>
string SysStrToStr(System::String^ sstr) {
        return msclr::interop::marshal_as< std::string >(sstr);
    }

Made that mistake:

1>Source.obj : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __clrcall SysStrToStr(class System::String ^)" (?SysStrToStr@@YM?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@P$AAVString@System@@@Z) already defined in MyForm.obj

Clarifying: I’m creating a CLR(Forms) application in Visual Studio 2012 (C++), and when I need to pass the contents of a Richtextbox(text) to a function, it sends System::string, and I always need to convert to "normal" string (Std::string) to use.

  • 1

    What do you call string normal? It would be System::String^? That’s the string more abnormal than I know :) If I were you I wouldn’t try to use .Net. C++CLI either. Net :) It’s a showcase that even MS doesn’t use (it even uses for interoperability but only, not to do something useful).

  • I’ll be more clear on the question. A moment.

  • I won’t be able to answer, but drop it, use decent technology :)

  • How so? '-'

  • read what I wrote above, this is an aberration of nature. Either do things in pure C++ or use C#.

  • Apparently you defined the function in a header ( .h file) and included it more than once in the project, resulting in double definition. Mark the function as inline resolves?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.