简体   繁体   中英

Iterating through C++ Vectors in a For loop

Something is wrong with my code. It is compiling, but the results are not what I am expecting, and although I can identify one of my problems, I wonder if I'm on completely the wrong track.

I've created a Class called test:

`
class test{

private:

//have moved the variables to public 
//so it's easy to see what's in the vector. 

public:

    int my_id, my_x,my_y,width, height;

test (int button_id=0, int x=0, int y=0, int width=0, int height=0)
{
    my_x = x;
    my_y = y;

    width = width;
    height = height;

}

~test()
{}

void handle_event()
{}

};

`

and now I want to fill a vector with these objects, and init their values from a text file.

This has been my approach:

int main(int argc, char const *argv[])
{

    //setup file
    ifstream inputFile("data.dat");
        const char* filename = "data.dat";
              std::ifstream inFile(filename);



    //init Vector
    vector<test> buttons;
        int id, x, y, width,height;
        int max_buttons = count(istreambuf_iterator<char>(inputFile),istreambuf_iterator<char>(), '\n');


    // Make sure the file stream is good
    if(!inFile) {
                cout << endl << "Failed to open file " << filename;
                return 1;
                }


  //Iterate fields into Vector,

  for (id=0 ; id < max_buttons ; id ++) 

  {

    inFile >> x >> y >> width >> height;

    buttons.push_back(test(id,x,y,width,height));    
    cout << std::setw(10) << x << y << width <<height <<endl;

  }

  cout << endl;

for(int p=0; p < 10; p++) // 

  cout << buttons[p].my_id << endl;

    return 0;
}

I've moved the variables within the class to public, so it would be easier to have a look at them, I will move them back once I've figured the problems out. Some fields are filling correctly (the x and y variables) but the id is not increasing with every call. I have a full vector but with nonsense data. I realise that parsing data directly from a text file means it will be in char format, and that is incompatible with integer type, but why isn't my ID increasing?

Thanks in advance!

here is the data:

23 16 10 19
24 40 10 17
23 16 10 16
25 40 10 14
26 16 10 10
27 40 10 12
27 36 10 11
28 40 10 13
29 34 10 18
27 49 10 10

You are not assigning my_id in the constructor of your test class.

Add this line to your constructor:

my_id = button_id;

You are not changing the id of the object in the constructor. So change it to the following

test (int button_id=0, int x=0, int y=0, int width=0, int height=0)
{
    my_id = button_id;
    my_x = x;
    my_y = y;

    width = width;
    height = height;

}

Try and compile with the warning flags (like -Werror , -Wall , -pedantic and so on) so that your code would have been flagged for things like unused variables in your constructor and you would have known your bug before it even happened!

Also you should not count the number of objects in the file by counting the number of new lines, that is problematic for two reasons

  1. What if there is no new line some places? For example after some objects.
  2. It requires looping over a file twice

You should loop like the following,

while(inFile >> x >> y >> width >> height) { ... }

This will return false when there is any of the 4 variables missing in the file as the istream will enter a "false" state and the loop will stop on its own

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM