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
#include<iostream>
#include<string>
#include"team.h"
usingnamespace 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
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.
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
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)