Dynamic arrays in OOP aggregation

hello , i used an array using aggregration in OOP in order to insert multiple datatypes in the array
and i wanted to convert it into dynamic array , so as to edit the entries
and here is the code of the 2 classes and the main function
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
#include<iostream>
#include<string>
using namespace std;


class player
{ 

private:

string name;
int age ;
int no;

public:

	player()
	{
	}

player(string name1 , int age1 , int no1)
		{ 
	  name=name1;
	  age=age1;
	  no=no1;
		}

	void print()
		{
	cout<<name<<endl;
	cout<<age<<endl;
	cout<<no<<endl;
		}
};

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
#include<iostream>
#include<string>
#include"Player.h"

using namespace std;

class team
{

private :

player  players[11];
string name;
int counter;
   

public:

team(string name1)
	{
	name=name1;
	counter=0;
	}

void addplayer(player p)
	{
 players[counter]= p;
	counter ++;
	
	}

	void print()
	{ 
		int i;
	for ( i=0;i<11;i++)
		{
players[i].print();
	}
	}



};

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
#include<iostream>
#include<string>
#include"team.h"
using namespace std;


int main()
{
	team A("abc");
int x;
		   string n; 
		   int a; 
		   int kn;
while(1==1)
{
cout<<"1.Add Player"<<endl;
cout<<"2.exit"<<endl;
	cin>>x;
switch(x)
{
case 1:

	


  cout<<"enter player info"<<endl;
  cin>>n>>a>>kn;
  A.addplayer(player(n,a,kn));
	
 A.print();
break;
case 2:
return 0;
break;
}
}
return 0;

}

the program is supposed to enter players & edit and delete them , with the static array i can just insert them , but i have to use dynamic arrays so i can edit and delete the data
plz help me , how can i convert it from a static array into a dynamic one
Last edited on
change the array declaration in private to this: player players[] // with empty bracket

then change the declaration of players addplayers to this: players = new[counter];

that's it !
dnambembe wrote:
player players[] // with empty bracket


Not quite....

player players[]; will not work because you cannot have empty brackets in a variable declaration. The only exceptions to this rule are:

If the declaration is followed immediately by an array initialization. Example:

1
2
3
4
5
6
void somefunction()
{
  int foo[];  // this will give you an error

  int bar[] = {1,2,3,4,5};  // this is OK.  The same as saying int bar[5] = {...}
}


The only other time empty brackets on var names is allowed is when passing as a parameter to a function -- in which case the brackets are the same as the * operator to indicate it's a pointer:

1
2
3
void func(int foo[])  { }  // OK

void func(int* foo) { }  // same thing as above, just different way of doing it 



If you want to allocate with new[], then the solution here would be to do this:

1
2
3
4
5
6
7
8
player* players;  // pointer, no [brackets]

//...
players = new player[ however_many_players_you_want ];

//...
//   when you no longer need the players
delete[] players;


Note that if you fail to delete[] the players, you will have memory leaks. Also note that once you determine a size for the array, it's not so straightforward to resize it... so doing what you're doing here where you keep adding new players -- you would probably end up resizing the array multiple times and it would be ridiculous and inefficient.



So the better solution here is to just use a vector:

1
2
3
4
5
6
7
8
9
10
11
vector<player> players;

// when you want to add a new player:
void AddPlayer(const player& p)
{
  players.push_back(p);  // that's all you have to do -- vector takes care of the resizing and everything for you
}

// you can still access individual players with the [] operator:

cout << players[3].name;  // like so 



And vectors will take care of the cleanup automatically so you don't have to worry about forgetting to delete[] or having memory leaks.


So yeah -- use vector.
Last edited on
i found these errors
31 F:\Dev-Cpp\2\team.h request for member `push_back' in `((team*)this)->team::players', which is of non-class type `std::vector<player, std::allocator<player> >[11]'


39 F:\Dev-Cpp\2\team.h 'class std::vector<player, std::allocator<player> >' has no member named 'print'

and this is the team.h header after modification in order to use vectors

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
#include<iostream>
#include<string>
#include"Player.h"
#include<vector>
using namespace std;

class team
{

private :

vector<player>  players[11];
string name;
int counter;
   

public:

team(string name1)
	{
	name=name1;
	counter=0;
	}

void addplayer(player &p)
	{
players.push_back(p);
	}

	void print()
	{ 
		int i;
	for ( i=0;i<11;i++)
		{
players[i].print();
	}
	}

};

how can i fix the errors ???

and when i use pointers this error is displayed
31 F:\Dev-Cpp\2\team.h ISO C++ forbids cast to non-reference type used as lvalue


and here is the code of the header team.h , while using pointers
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

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

class team
{

private :

player *players;
string name;
int counter;
   

public:

team(string name1)
	{
	name=name1;
	counter=0;
	}

void addplayer(player p)
	{
players = new player[11];


new player[counter]= &p;
	counter ++;
	
	}

	void print()
	{ 
		int i;
	for ( i=0;i<11;i++)
		{
players[i].print();
	}
	}



};
Last edited on
vector<player> players;// [11]; <-- get rid of that [11]

And your header with using pointers would have memory leaks, and doesn't allow you to have more than 11 players. (and has other errors -- like you using 'player' as a variable name when it's already a class name)
Topic archived. No new replies allowed.