DarthVader
DarthVader

Reputation: 55062

Accessing struct element in a vector

I have vector of elements,

vector<ImageData> data ;

here s the definition of ImageData

#ifndef IMAGEDATA_H
#define IMAGEDATA_H
#include <iostream>
#include <vector>
using namespace std;
struct ImageData {

                std::string cx;
                std::string cy;
                std::string cw;
                std::string ch;
                std::string ht;
                std::string wd;
                std::string ut;
                std::string rip;
                std::string imgFormat;
                std::string dIITest;
                vector<std::string> imageUrls;

                ImageData()
                {
                        cx="";
                        cy="";
                        cw="";
                        ch="";
                        ht="";
                        wd="";
                        ut="";
                        rip="foo";
                        imgFormat="";
                        dIITest="";
                        imageUrls.clear();
                }
};

on another method, when i try to access this struct elements as follows I get back segmentation fail.

for(int i = 0 ; (int) data.size();i++)
{
    cout<<data[i].rip;
}

essentially this code dumps.

Why is this happening ?

any ideas?

Upvotes: 1

Views: 1858

Answers (1)

hmjd
hmjd

Reputation: 122001

for loop should be:

for (size_t i = 0; i < data.size(); i++)
                 //^^^

Upvotes: 4

Related Questions