Wierd string/class error

This is a discussion on Wierd string/class error within the C++ Programming forums, part of the General Programming Boards category; Code: class CEmployee { public: string name; Shift sArray[7]; } class Shift { public: int type; float start, end; Shift() ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    22

    Question 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.

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Post a complete, compilable example that demonstrates the problem.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    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();
    
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21