Im trying to create an object array that accepts values generated by a 2 random functions so that i can sort the objects in the array based on one value of an identifier (finishtime) in each object.

eg. twenty Activitys with datamembers [starttime, finishtime, id]

my code for the main.cpp file looks like this

#include "Activity.h"

//#define SIZE 20  //This is a GLOBAL SYMBOLIC CONSTANT

void swap(int X[], int i, int j);
void printData(int[], int);
void printSortedData(int[], int, int);
void bubbleSort(int A[], int size);


int randomStart(int x);
int randomFinish(int y);

int main()
{
	srand(time(0));
	int z;
	int w;
	int start[20];
	int finish[20];
	


	for (int i=1;i<21;i++)
	{
		start[i] = randomStart(z);
		finish[i] = randomFinish(w);
		

		Activity Acts[i](start[i],finish[i],i);
		Acts[i].display();	
		
	
	}
	
	int ActivityArray[20]={};


	//cout<<"\n Sorting with Bubble Sort:\n";
	//bubbleSort(Data, SIZE);
	return 0;
}


int randomStart(int x)
{
	x=0+rand()%69;		
	return x;
	
}

int randomFinish(int y)
{
	y = 1 + rand()%(70-1)+1;
	return y;
}

and i recieve the following errors

1>.\activity-main.cpp(30) : error C2057: expected constant expression [LINE 30]
1>.\activity-main.cpp(30) : error C2466: cannot allocate an array of constant size 0 [LINE30
1>.\activity-main.cpp(30) : error C2075: 'Acts' : array initialization needs curly braces [LINE 30]

You want an array of size i, but i is a runtime variable. The compiler is giving you this warning because it has no idea how much space to allocate for Acts.

You should look up dynamic memory allocation, or re-design your solution using STL containers.

This article has been dead for over six months. Start a new discussion instead.