-
Wierd string/class error
Code:
class CEmployee {
public:
string name;
Shift sArray[7];
}
class Shift {
public:
int type;
float start, end;
Shift() : start(0), end(0), type(0) {};
void StrInit(string s) {
type = -1;
}
};
vector<CEmployee> empList(7);
string s;
for(j=0; j<7; j++) {
empFile >> s;
empList[i].reqShift[i].StrInit(s);
}
If I keep the empList[i].reqShift[i].StrInit(s); line in my program, I get an access violation during the deconstructor of the string of CEmployee. If I remove the name parameter or don't include that line, I have no problems.
-
You're using j as the variable of your loop, but I see you using i as the subscript in both cases.
And I don't see any reqShift member of your CEmployee class.
-
Post a complete, compilable example that demonstrates the problem.
-
Yeah, sorry about that, I just cut and paste code to make it shorter.
Code:
class CEmployee {
public:
string name;
Shift reqShift[7];
}
class Shift {
public:
int type;
float start, end;
Shift() : start(0), end(0), type(0) {};
void StrInit(string s) {
type = -1;
}
};
int main() {
vector<CEmployee> empList(7);
string s;
fstream empFile;
empFile.open("file");
if( empFile.fail() )
return -1;
for(int i=0; i<7; i++) {
for(int j=0; j<7; j++) {
empFile >> s;
empList[i].reqShift[j].StrInit(s);
}
}
empFile.close();
}
-
That's not really compilable, but when I made changes to get it to compile it worked without access violation. The code itself looks fine other than the compile errors (missing #includes, missing semi-colon, order of class declarations).