Strange problem with looping through vectors

This is a discussion on Strange problem with looping through vectors within the C++ Programming forums, part of the General Programming Boards category; I am trying to loop through a vector to check the values against the actual file. At this point I ...

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    Strange problem with looping through vectors

    I am trying to loop through a vector to check the values against the actual file. At this point I know that they work because I have checked them as they were being read in and I have checked them individually, but when I check all of them at the same time it doesn't work. To clarify 'they' are points, and when I check each point individually it works fine, but when I check them all at the same time I get incorrect values. Here is the code I am using, the second point is commented out on purpose.

    Code:
    			for(int j = 0; j < mnumbrushes; j++){
    			//currentplane is really now the number of planes because we have already read them all in from the file
    				for(int i = 0; i < BrushVector[j].currentplane; i++) {
    				cout << "Plane number: " << i << " of brush number " << j << endl;
    				cout << "X value p1: " << BrushVector[j].PlaneVector[i].p1.x << endl;
    				cout << "Y value p1: " << BrushVector[j].PlaneVector[i].p1.y << endl;
    				cout << "Z value p1: " << BrushVector[j].PlaneVector[i].p1.z << endl;
    
    			//	cout << "X value p2: " << BrushVector[j].PlaneVector[i].p2.x << endl;
    			//	cout << "y value p2: " << BrushVector[j].PlaneVector[i].p2.y << endl;
    			//	cout << "z value p2: " << BrushVector[j].PlaneVector[i].p2.z << endl;
    		
    				cout << "X value p3: " << BrushVector[j].PlaneVector[i].p3.x << endl;
    				cout << "y value p3: " << BrushVector[j].PlaneVector[i].p3.y << endl;
    				cout << "z value p3: " << BrushVector[j].PlaneVector[i].p3.z << endl;
    			}	//PLANE
    				//BRUSH
    	}			//FUNCTION
    any advice will be appreciated

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Code seems valid.

    Post an example of of objects creations including one PlaneVector and BrushVector. I want to see how you instantiate the objects and insert them into the containers.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Using .size() rather than numbrushes and currentplane could also be a sanity check. You could also use .at() rather than [] to see if it throws anything. Best guess from what you have shown is either a read out of bounds or an invalid copy ctor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  2. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 03:55 AM
  3. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  4. problem with sdl and vectors
    By r0bbb in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2005, 05:11 AM
  5. Strange problem with fts_open call
    By jhopper in forum C Programming
    Replies: 0
    Last Post: 02-25-2002, 11:01 PM

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