0

I have to create a small gui project using Visual Studio 2012 forms, the major problem: I can't create an array of objects for my life, not even the fellow students' projects run.

array<className^>^ arrayName = gcnew array<className^>(100);

Or in my case:

array<kind^> ^testArray = gcnew array<kind^>(100);

should be working. Should. The error is somewhat like

Error: data member initializer is not allowed

It works for others but somehow not for me in vs2012, even though it's exactly the same. Don't get what I'm missing.

Quick and dirty example of a classes.h I created to test stuff:

ref class memberino {
private:
    String^ memVar;
public:
    memberino(){};
    memberino(String^ memVar_);
    String^ getMemVar() { return memVar; }
    void setMemVar(String^ memVar_) { this->memVar = memVar_; }
};

ref class baseClass abstract {
private:
    String^ baseVar;
    memberino M;
public:
    baseClass(){};
    baseClass(String^ baseVar_, String^ memVar_);
    virtual String^ getMemVar() { return M.getMemVar(); }
    virtual String^ getBaseVar() { return baseVar; }
    virtual void setBaseVar(String^ baseVar_) { this->baseVar = baseVar_; }
    virtual String^ getChildVar() = 0;
};

ref class kind: public baseClass {
private:
    String^ childVar;
public:
    kind(){};
    kind(String^ baseVar_, String^ memVar_, String^ childVar_);
    virtual String^ getChildVar() override { return childVar; }
    virtual void setChildVar(String^ childVar_) { this->childVar = childVar_; }
};

And the classes.cpp:

memberino::memberino(String^ memVar_){
    this->setMemVar(memVar_);
}
baseClass::baseClass(String^ baseVar_, String^ memVar_):M(memVar_){
    this->setBaseVar(baseVar_);
}
kind::kind(String^ baseVar_, String^ memVar_, String^ childVar_):
baseClass(baseVar_, memVar_){
    this->setChildVar(childVar_);
}

In the MyForm.h I just try to create an array like:

array<kind^> ^testArray = gcnew array<kind^>(100);

Before trying to run it it's red underlined with the error mentioned above. Compiling it gives following output:

warning C4677: "testArray": The signature of the non-private member includes the private assemblytype "kind".

error C3845: "pr5temp::MyForm::testArray": Only static data members can be initialized in a reference class or value type

Not 100% as it's a translation from German to english, but in case it helps:

warning C4677: "testArray": Die Signatur des nicht privaten Members enthält den privaten Assemblytyp "kind".

error C3845: "pr5temp::MyForm::testArray": Nur statische Datenmember können innerhalb einer Verweisklasse oder eines Werttyps initialisiert werden.

4
  • Show minimal code that demonstrates your bug. Commented Jun 21, 2015 at 19:12
  • 1
    Please copy the actual error, and paste it here, complete and unedited. Commented Jun 21, 2015 at 19:14
  • Tried to translate it, not sure if that's the exact meaning, I'm sorry. Commented Jun 21, 2015 at 19:33
  • You declare the array member public inside a public class but code in another module cannot use it since the "kind" type is not public. Initialize the array in the constructor. Commented Jun 21, 2015 at 21:07

1 Answer 1

1

You should initialize field in constructor.

    ...
        private:
            System::ComponentModel::Container ^components;
            array<kind^> ^testArray;
    ...
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            testArray= gcnew array<kind^>(100);
        }
    ...

or indeed make it static

static array<kind^> ^testArray = gcnew array<kind^>(100);

but keep in mind that the elements in the array has not been initialized.

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

2 Comments

Hmm, your advice is correct, but that code doesn't initialize the field, it assigns it. Initialization looks like MyForm(void) : testArray(gcnew array<kind^>(100)) { InitializeComponent(); }
@BenVoigt , thank for your comment. In the future I will more (thoroughly)carefully treat the terminology

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.