none
Error C4368: mixed types are not supported

    Question

  • I'm trying to move towards Windows Forms and managed code(In VS2005) from a simpler console c++ background back in university. The code I was trying to compile was more complicated than this but the snippet below demonstrates the problem. I can use 10 ints seperately and the code works fine, but its an issue later.
    I've done some reading around the subject, I understand this sort of code not working is a design choice to stop issues people have experienced writing complicated code(been a while since I learnt anything on memory management).
    Can anyone provide me with a simple solution to be able to use "global" arrays of a fixed size within a windows form?
    Seems like an obvious thing to need to do, access an array from anywhere within a form?!

    Compiler message I'm getting in full is:
    Form1.h(16) : error C4368: cannot define 'numbers' as a member of managed 'Test::Form1': mixed types are not supported

    Code Snippet


    #pragma once

    namespace Test {

        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;

        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
            int numbers[10];
            Form1(void)
            {

                InitializeComponent();

            }

        protected:
            ~Form1()
            {
                if (components)
                {
                    delete components;
                }
            }

        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            void InitializeComponent(void)
            {
            this->SuspendLayout();
            //
            // Form1
            //
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(292, 271);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);

                }
    #pragma endregion
        private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                     numbers[0] = 10;
                     }
            };
    }

    Sunday, April 29, 2007 8:31 PM

Answers

All replies

  • Hi,

    CLR array declaration is different from unmanaged code(standard C++/C).

     

    Please reference this page.

    http://msdn2.microsoft.com/en-us/library/ms235236(VS.80).aspx

    Sunday, April 29, 2007 8:56 PM
  • Thanks a lot for the fast answer. Thats exactly the sort of page I was looking for (Especially the link at the bottom), I'm impressed you knew what I was trying to say better than I did!

    Following the example on that page the new code read:
    Code Snippet


    array<int,1>^ numbers = gcnew array<int, 1>(10);


    I then had to declare it static, which seems right judging from some old code I had and the explanation on there.

    It compiles fine which is a good sign. Thats correct isnt it? A 1 dimensional array of integers with 10 "cells".

    Thanks again
    Sunday, April 29, 2007 9:44 PM