Hi,

I am really confused. Everywhere I read on the internet, I see that the += operator is part of the string class. I want to add something onto the end of the string. Why does my code not compile? I keep getting the C2088 error: illegal for class.

If the solution has to be completely redone, here is the file:

#include "stdafx.h"
#include <string>
using namespace std;
using namespace System;

String ^Convert(String ^OriginalText) {
string Array[13];
String ^ConvertedText;

for (int Loop = 0; Loop < 13; Loop++)
{
	ConvertedText += Array[Loop];
	ConvertedText += '\n';
}

return ConvertedText;
}

I just took out the unnecessary bits. Do I need a variant of the concat function? Please help!

c++/CLR String class is not the same as c++ string class declared in <string> header file. Look up system::String class and review its methods

You can not use <string> in CLR/C++ programs, so you might as well delete that header file.

Example:

using namespace System;

int main(array<System::String ^> ^args)
{
    String^ s = gcnew String("Hello ");
    s = String::Concat(s," World");
    Console::Write(s);
    Console::Read();
    return 0;
}

Fixed, thank you.
Array defined slightly differently.

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    string name;
    string name2 = "Test";
    
    cin>>name;
    cout<<"string : "<<name<<endl;
    name.append(name2);
    cout<<"string : "<<name<<endl; 
    
    system("pause");
    return EXIT_SUCCESS;
    
}

this work to for using string lib

Yes, but I'm using System::String^ and not std::string - With System::String^ the += operator is supported, but defining my array is different.

According to this the += operator is not supported. Can you provide a link that says otherwise?

Don't have a link, but this compiles and runs perfectly.

String ^ParseBBCode(String ^OriginalText) {
array<String ^> ^Array = gcnew array<String ^>(13){ "\n", "Hello", "\n", "World", "", "", "", "", "", "", "", "", "" };
String ^ConvertedText;

String ^Line1 = "";

for (int Loop = 0; Loop < 13; Loop++)
{
	ConvertedText += Array[Loop];
}

EDIT: Newline character isn't working. Perhaps help? Maybe '\n'.toString()?

commented: nice example code. +17

Side note: newline character fixed, just needed a \r before it... silly me. :P

So... should I mark this as solved?

If so - the final result of this thread is that the std::string and System::String^ classes are not compatible.

And the fixed file from the original thread post is as follows:

#include "stdafx.h"
using namespace System;

String ^Convert(String ^OriginalText) {
array<String ^> ^Array = gcnew array<String ^>(13){ "", "", "", "", "", "", "", "", "", "", "", "", "" };
String ^ConvertedText;
 
for (int Loop = 0; Loop < 13; Loop++)
{
	ConvertedText += Array[Loop];
        ConvertedText += "\r\n";
}
return ConvertedText;
}

This is for C++/CLI with Common Language Runtime Support (clr). I suggest using std::string for a console application! I am using a Windows Form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.