Error while creatingt the array of the class dynamically .

Hi , getting error while executing the progam , does not allow the array of the class to be crated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// VitrualFunction070720120023.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std; 

class Person
{
private:
	int a ; 
	virtual void display() {}
public:
	Person() { } 
	void m_display() { display() ; }
	virtual ~Person() { } 
};

class Student : public Person
{
private:
	int b ; 
public:
	Student() { } 
	~Student() { } 
	void display() { cout <<"\n This is the display for the student "; }

};
class Professional : public Person
{
private:
	int c; 
public:
	Professional() {  } 
	~Professional() { } 
	void display() { cout<<"\n We are the professional "; } 
};


//deriver function 
void Test( Person* per ) 
{
	per->m_display();
}

//Main function 
int _tmain(int argc, _TCHAR* argv[])
{
	Person[] person = { new Student() ,  new Professional() };
	for( int i = 0 ; i < 2 ; i++)
	{
		Test( person[i] ) ; 
	}
	for( int i = 0 ; i < 2 ; i++)
	{
		delete person[i] ; 
	}
	
	return 0;
}

 


giving the error as


projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2143: syntax error : missing ';' before '['
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C3409: empty attribute block is not allowed
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2146: syntax error : missing ';' before identifier 'person'
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2065: 'person' : undeclared identifier
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2059: syntax error : '{'
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2143: syntax error : missing ';' before '{'
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2143: syntax error : missing ';' before '}'
1>Build log was saved at "file://j:\Projects\VitrualFunction070720120023\VitrualFunction070720120023\Debug\BuildLog.htm"


Question is how do we create the array of the object dynamically .

please throw some light on it .. i have been surfing on net but not able to get the concept .
thanks in advance .
Last edited on
Looks like you are trying to mix in some C# there.

The proper way to do it is like this:

1
2
3
4
5
6
7
8
Person* person[2] = { new Student(), new Professional() };

// ..

for( int i = 0 ; i < 2 ; i++)
{
    delete person[i] ; 
}


Or... if you also want the size of the array to be dynamic....

1
2
3
4
5
6
7
8
9
10
11
12
int x = /*number of people*/
Person** person = new Person[x];
for(int i = 0; i < x; ++i)
{
  person[i] = new Whatever();
}

//..

for(int i = 0; i < x; ++i)
  delete person[i];
delete[] person;



EDIT:

or... better yet ... use smart pointers and containers:

1
2
3
4
5
6
7
typedef std::shared_ptr<Person> PersonPtr;
std::vector< PersonPtr > people;

people.push_back( PersonPtr( new Student() ) );
people.push_back( PersonPtr( new Professional() ) );

// all cleanup is done automatically.  No need to delete anything 
Last edited on
Topic archived. No new replies allowed.