user3883278
user3883278

Reputation: 47

Error: expression must have class type?

probably such a simple fix, but I cannot seem to figure out what the issue is. the 2nd line is what is having the issue, what did I do wrong?

System::String^ userinfo = DownloadHTMLPage("http://xat.com/web_gear/chat/auser3.php");
String^ str2 = gcnew String(userinfo.c_str());

Upvotes: 1

Views: 1670

Answers (2)

David Yaw
David Yaw

Reputation: 27864

System::String and String are the same class. Since .Net strings are immutable, you don't need to make another one using gcnew, you can just use userinfo directly.

String^ userinfo = DownloadHTMLPage("http://xat.com/web_gear/chat/auser3.php");

// Use userinfo in your code, or...
String^ str2 = userinfo;

Based on the comments on the other answer, it looks like you're getting C++ strings and .Net strings confused. They are not the same class!

To convert from one to the other, use marshal_as, like this:

#include <marshal_cppstd.h>

String^ managed = "foo";

std::string unmanagedNarrow = marshal_as<std::string>(managed);
std::wstring unmanagedWide = marshal_as<std::wstring>(managed);

String^ managed2 = marshal_as<String^>(unmanagedNarrow);
String^ managed3 = marshal_as<String^>(unmanagedWide);

Upvotes: 0

Alireza
Alireza

Reputation: 5056

userInfo is a pointer, so you should write that line as

System::String^ str2 = gcnew System::String(userinfo->c_str());

Upvotes: 3

Related Questions