Growing structure arrays?
I made a structure and I delcared it as an array so now I need to go through a loop and add one more level to the array every time. The real problem is that this isn't VB so I can't redim preserve lol. But besides that, how could I code it in C++? I could have sworn it went like:
Code:
Mystruct struct[1]; //to declare it
//...inside the loop:
Mystruct *struct = new Mystruct[2];
but that gives me an error saying:
error C2372: 'struct' : redefinition; different types of indirection
That should just go to the memory location of the array and allocate more for it but nooo. I looked up the error in MSDN and it makes sense why it exists but it doesn't make sense why it's happening to me now with that code. So what's wrong with it?
Re: Growing structure arrays?
Use another name for the pointer, struct is a reserved word (you used it above)
Re: Growing structure arrays?
oops :P well I changed it to the structure name being Employee and the structure instance being emps and it still gives me the same error :(
Re: Growing structure arrays?
struct declaration:
Code:
struct Employee
{
char name;
};
Instance:
Code:
Employee *emps = new Employee[2];
Re: Growing structure arrays?
hmm yeah that's pretty much what it looked like before when it didn't work :( but now that I moved the re-declaration or redefinition or whatever statement inside the loop where it should be, it works. Straaaange. Of course, works is a relative term. Now something else is broken :( My current code looks like this:
Code:
struct Employee
{
int number;
char name[21]; //20 sounds about right for a name
float rate;
float hours;
};
char cemps[10] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
cout << "How many employees would you like to enter?" <<endl;
cin.getline(cemps,10,'\n');
iemps = atoi(cemps);
Employee emps[1] = {0,{NULL},0.0,0.0};
for (int x = 1; x <= iemps; x++)
{
Employee *emps = new Employee[x];
//then a bunch of code to fill the structure with data
}
and it doesn't give me an error but the first time though, it fills spot [0] of the array with the data I enter. Then it completely wipes out the data in array spot [0] and replaces it with random garbage on the second loop and fills spot 1 with the proper data. Then on the third loop, it replaces spot [0] and spot [1] of the array with random garbage and fills spot [2] correctly. So basically it's erasing my array every time it hits the Employee *emps = new Employee[x]; command. So how can I get it to not do that?
Re: Growing structure arrays?
You're creating two different variables with the same name. When you put it inside the loop, you create it in a different scope, which is why you got rid of the error.
Of course, what you're trying to do won't work at all, and leaks memory every iteration of the loop. The best way to accomplish what you want to do is to use a vector.
Code:
#include <string>
#include <vector>
#include <iostream>
using namespace std;
struct Employee
{
int number;
string name;
float rate;
float hours;
};
int main()
{
int emp_count;
vector<Employee> emps;
cout << "how many employees?" << endl;
cin >> emp_count;
emps.resize(emp_count);
for(int i = 0; i < emp_count; i++)
{
// fill emps[i] with data.
}
}
If you have can't use string/vector/etc (ie, this is a school assignment) then what you need to do is allocate the array like so:
Code:
int emp_count;
cout << "how many employees?";
cin >> emp_count;
Employee* emps = new Employee[emp_count];
for(int i = 0; i < emp_count; i++)
{
// fill emps[i] with data.
}
delete[] emps;
Hope this helps!