0

I am working in Visual C++. I'm giving following command

String nodename[100];

but this command is giving the following error

"error : 'System::String' : a native array cannot contain this managed type"

So what should I do now ?

1
  • 3
    If you don't know what native arrays and managed types are, you should probably not bother with managed C++ at all and use C# instead :) Commented Sep 30, 2009 at 17:42

4 Answers 4

4

If you want to write a native C++ application, then you can't, as the error says, use managed types.

That means you have to use the C++ string class,

#include <string> // at the top of the file
std::string nodename[100]; // where you want to declare the array

instead of System::String.

On the other hand, if you want to make a managed C++/CLI application, then you can't use native arrays. (But can use all the .NET types)

Sign up to request clarification or add additional context in comments.

3 Comments

the above command gives the following error 'std' : is not a class or namespace name
Then you didn't add the #include part properly. Put that at the very top of the file.
and it is string, not string.h
3

You didn't say whether you want Managed C++, C++/CLI, or unmanaged C++

managed C++

http://www.codeproject.com/KB/mcpp/csarrays01.aspx

C++/CLI

http://www.codeproject.com/KB/mcpp/cppcliarrays.aspx

unmanaged C++ (standard C++)

std::string nodename[100]; // uses STL string, not .NET String

1 Comment

i want to use unmanaged C++ std::string nodename[100]; this command give following error even though i use #include"string.h" also 'std' : is not a class or namespace name
0

OK. First off it looks like you are using managed C++ (ick). Secondly, you are trying to declare an array of strings (not chars, but entire strings). Is that really what you want to do? Are you sure you don't want something like char nodename[100] or just String nodename?

If you really want an array of strings, it looks like the compiler either wants you to use one of its managed vector-like types to do it, or to use only unmanaged types to do it.

Comments

0
#include "stdafx.h"
#include <conio.h>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    string name[5] = {"Marco","Jenna","Alex","Dina","Allyson"};
    int index = 5;

    printf("List:\n\n");
    for (int i = 0; i < index; i++)
    {
        cout << name[i] << endl;
    }
    _getch();
    return 0;
}

1 Comment

Can you elaborate on your answer? Posting a piece of code without any text is often not very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.