vector::_M_range_check

Jul 4, 2015 at 6:15am
Why does this happen my program says that it has no errors but then when I run it and I value sn , temp; I get terminate called after throwing an instance of 'std::out_of_range' what(): vector:_M_range_check. I am new to c++ so I don't understand these errors

here's my function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "Region.h"
using namespace std;
namespace personalization ;
    int main() {

        int t ;
        for ( cin >> t ; t != 0; t--){     // reads t different test cases .

            Region region ;

             while(true)
            {
                 int sn , temp;
                 cin >> sn >> temp;
                 if (sn == 0 && temp == 0)
                     break;
                 region.add_sensor(sn , temp) ;
}
                 cout<<.......<<endl;
}
return 0;
            }


Region.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Region.h"
#include "Sensor.h"
#include "Network.h"
namespace personalization {
int Region::index;

void Region::add_sensor(int s,int t){
    Sensor sensor (s,t);
    Network network;
    network.add(sensor);
    r_networks[index]=network;
    index++;
}

Network.cpp:

1
2
3
4
5
6
7
8
9
#include "Network.h"
#include "Sensor.h"
#include <vector>
namespace personalization {
int Network::index=0;
void Network::add(Sensor new_sensor){
	sensors.at(0)=new_sensor;
	index =1;
}
Last edited on Jul 4, 2015 at 8:28am
Jul 4, 2015 at 7:46am
Why does this happen my program says that it has no errors

No. Your compiler says your program is syntactically correct. It does not say that your program contains no errors.

When a Network is constructed how is its sensors member initialized?

When a Region is constructed, how is its r_networks member initialized?

What types are these two members?

You've given very little relevant information/code. In the future, please try to provide a complete but minimal code sample that is compilable and produces the problem you're experiencing. Doing so means you're more likely to get a cogent response.
Jul 4, 2015 at 9:07am
Sensor get value from user
r_networks[index] ;index=0;
1
2
`vector<Network> r_networks;`
`vector<Sensor> sensors;`
Last edited on Jul 4, 2015 at 9:08am
Jul 5, 2015 at 4:32am
You still haven't provided the information I asked for.

You would encounter the problem you're having if sensors was empty when it was indexed with at(0), since 0 wouldn't be a valid index. r_networks probably wouldn't have the same result if it were empty, since you're using unchecked access, although some compilers may check the access anyway in debug mode.
Jul 5, 2015 at 4:53am
in this line sensors.at(0)=new_sensor; i say new sensor be in array[0] and index increase
Jul 5, 2015 at 4:57am
Region.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #ifndef REGION_H_
    #define REGION_H_
    #include "Sensor.h"
    #include "Network.h"
    namespace personalization {

    class Region {
    public:
    	Region();
    	void add_sensor(int, int);
    	virtual ~Region();
    private:
    	vector<Network> r_networks;
    	static int index;
    };

    } /* namespace personalization */

    #endif /* REGION_H_ */ 

Last edited on Jul 26, 2015 at 5:29am
Jul 5, 2015 at 1:07pm
You can use theinsertfunction to add elements.
Theoperator[]andatdon't construct elements,so you shouldn't use them with an invalid index.
Topic archived. No new replies allowed.