Link to home
Start Free TrialLog in
Avatar of LesterClayton
LesterClaytonFlag for Norway

asked on

Need help converting Array of Member Variable to Vector

Consider the following class:

namespace GuiAppTest01 {
	public ref class CatClass {

	private:
		System::Windows::Forms::GroupBox^  groupBox1;

	public:
		void AddCat(System::Windows::Forms::Panel^ Panel);
	};

	void CatClass::AddCat(System::Windows::Forms::Panel^ Panel)  {
		this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
		this->groupBox1->Parent = Panel;
		this->groupBox1->Dock = System::Windows::Forms::DockStyle::Top;
		this->groupBox1->Location = System::Drawing::Point(0, 0);
		this->groupBox1->Name = L"groupBox1";
		this->groupBox1->Size = System::Drawing::Size(20, 40);
		this->groupBox1->TabIndex = 0;
		this->groupBox1->TabStop = false;
		this->groupBox1->Text = L"I'm a cat!";
	}
}

Open in new window


I am able to create and assign an array of variables using this class like such:

array<CatClass ^> ^ Cats;

Open in new window


And inside the Constructor, I initialiase the memory like so:

Cats = gcnew array<CatClass ^>(2);

Open in new window


Using this array is easy, by simply allocating memory for each element and then using it, like this:

				Cats[0] = (gcnew CatClass);
				Cats[0]->AddCat(this->panel1);
				Cats[1] = (gcnew CatClass);
				Cats[1]->AddCat(this->panel1);

Open in new window


The problem is that we all know that Arrays are evil as they cannot be increased on the fly - and the recommended solution of course is to use vectors.

How would I go about creating a vector of this reference class and using it?  I've tried all sorts of combinations of stuff, but I get various errors - as follows:

std::vector<CatClass>

Open in new window

IntelliSense: a using-declaration or access declaration cannot appear in a managed class

std::vector<CatClass^> CatVector;

Open in new window

IntelliSense: a member of a managed class cannot be of a non-managed class type

std::vector<CatClass> & CatVector;

Open in new window

error C2758: 'GuiAppTest01::MainForm::CatVector' : must be initialized in constructor base/member initializer list

All of the searches I've done have less complex variable types exampled in vectors, so I need to ask the experts.  Using Visual Studio 2012 Express
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LesterClayton

ASKER

Thank you :)  You have helped me a great deal.  This is what I've got now:

	
		List<CatClass^> ^CatList;
		CatList = gcnew List<CatClass ^>;    // Inside the Constructor

		CatClass ^cat;                       // Declare a new variable
		cat = (gcnew CatClass);              // Allocate memory
		cat->AddCat(this->panel1);
		CatList->Add(cat);                   // Store in list :D

Open in new window

Thanks for the helpful tip :)