Dynamically allocating classes with dynamically allocated arrays

Started by
2 comments, last by APCJim 18 years, 7 months ago
Hi; I've gotten the following program to run fine in MS VC++.NET, but I need to break it up so it will operate via events in a GUI. The working program is: #include "stdafx.h" #using <mscorlib.dll> using namespace System; __gc public class Response_Data { public: String* Name; int Length; double RArray __gc[]; }; int _tmain() { Response_Data *Response __gc[] = new Response_Data * __gc[2]; Response[0] = new Response_Data; Response[0]->Name = S"Response[0]"; Response[0]->Length = 5; Response[0]->RArray = new double __gc[Response[0]->Length]; for(int i = 0; i < Response[0]->Length; i++) { Response[0]->RArray = double(i); } Console::WriteLine(Response[0]->Name); for(int i = 0; i < Response[0]->Length; i++) { Console::WriteLine(Response[0]->RArray); } Response[1] = new Response_Data; Response[1]->Name = S"Response[1]"; Response[1]->Length = 15; Response[1]->RArray = new double __gc[Response[1]->Length]; for(int i = 0; i < Response[1]->Length; i++) { Response[1]->RArray = 3.0 * double(i); } Console::WriteLine(Response[1]->Name); for(int i = 0; i < Response[1]->Length; i++) { Console::WriteLine(Response[1]->RArray); } return 0; } The issue is that I need to use the Response_Data class as an adjustable array within a Process_Variable class. The user will add additional Response_Data to the Process_Variable when the program is running, via a GUI. It will start out with 0 elements and the user will add one Response_Data item at a time and then set the length of the Response_Data RArray. I already figured out how to resize these after they are created so that's not an issue. Since the user will be adding these via a mouse click event I believe that means I will have to break this program into the following parts: A function that runs when the Process_Variable class in instantiated: Response_Data *Response __gc[] = new Response_Data * __gc[2]; Another function that allows the user to add a response vector via a mouse click: Response[int] = new Response_Data; Response[int]->Name = S"Response[int]"; Response[int]->Length = some_length; Response[int]->RArray = new double __gc[Response[0]->Length]; for(int i = 0; i < Response[int]->Length; i++) { Response[int]->RArray = some_calc; } No matter how I try to break this up, as functions or methods in another class it fails to compile. Any ideas on how I can split this into parts and get it to compile? Any help on this would be greatly appreciated.
Advertisement
It would help if you gave use the errors that your compiler producing.

Mark
Can you give me a picture of what's your classes do and their name ?. I'm confused.
By the way, show me that error message.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Thank you all for looking. I just managed to get it to work.

I had deleted the copies of the program that wouldn't work. To reproduce the problem and its errors I modified the working program to the point it was not compiling; but this time it compiled. I'm sure I previously tried this program configuration without success, but obviously not exactly how I did this time.

If you're curious, this is the version that compiled:

HEADER FILE:

#pragma once
#using <mscorlib.dll>
using namespace System;

__gc public class Response_Data
{
public:
String* Name;
int Length;
double RArray __gc[];
};

__gc public class Variable_Data
{
public:
// Members
Response_Data *Response __gc[];

// Methods
void init_Response(int num_Resp);
void add_Response(int new_Resp, int new_Length, String *new_Name);
};

CLASS METHOD DEFINITION FILE:
#include "stdafx.h"
#include "Structure.h"

void Variable_Data::init_Response(int num_Resp)
{
Response = new Response_Data * __gc[num_Resp];
}

void Variable_Data::add_Response(int new_Resp, int new_Length, String *new_Name)
{
Response[new_Resp] = new Response_Data;
Response[new_Resp]->Name = new_Name;
Response[new_Resp]->Length = new_Length;

Response[new_Resp]->RArray = new double __gc[Response[new_Resp]->Length];

// populate RArray
for(int i = 0; i < Response[new_Resp]->Length; i++)
{
Response[new_Resp]->RArray = double(i);
}

// print RArray
Console::WriteLine(Response[new_Resp]->Name);
for(int i = 0; i < Response[new_Resp]->Length; i++)
{
Console::WriteLine(Response[new_Resp]->RArray);
}
}

MAIN:
#include "stdafx.h"
#include "Structure.h"
#using <mscorlib.dll>
using namespace System;

int _tmain()
{
Variable_Data *Variable = new Variable_Data;
Variable->init_Response(2);
Variable->add_Response(0, 5, S"Response_1");
Variable->add_Response(1, 15, S"Response_2");

return 0;
}

This topic is closed to new replies.

Advertisement