i+1 index of int's vector

Hello I've big problem with my vector... I'd like to get the value from it, but not i index but i+1 index. Compilator shows this:
'lvalue required as left operand of assignment'

My vector has 4 int fields and need to change those values during the algorithm work. Here's my code. I signed trouble lines by '/////'.
Could somebody show me how to resolve this issue? I've read that Get and Set methods can be usefull but I don't know how to implement them... I'll be veeeery glad that somebody fix M1[j+1].Time() that it'll returns needed value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cstddef>
#include <cstdlib>
#include <iterator>
#include <algorithm>
using namespace std;
class Item
  {
   public:
   int time, dist, index, deadline;
   Item(int time=0, int dist=0, int index=0, int deadline=0):time(time),dist(dist), index(index), deadline(deadline) {}
   int Time()const { return time; } //a
   int Dist()const { return dist; } //b
   int Index()const { return index; } //c
   int Deadline()const { return deadline; } //d
  };
  void permutation(int k, int n, vector<Item> M1, vector<Item> M1_wh)
{
    int result=0, diff = 0;
    if(k==0)
        {
            int g=0, j=0;
            for(int i=0; i<n; i++)
            {
                for(j=j+g; j<n; j++)
                {
                    if(M1_wh[i].Dist()==M1[j].Dist())
                    {
                        if(M1_wh[i].Index()==0)
                        {
                        result = M1_wh[i].Time() + M1[j].Time();
                        M1[j+1].Time() = M1[j+1].Time() + result;// here it starts, every place where it's j+1 or i+1 :((
                        g++;
                        }
                        else
                        {
                        result = M1[j].Time();
                        M1[j+1].Time() = M1[j+1].Time() + result;
                        g++;
                        }
                    }
                    if(M1_wh[i].Dist()>M1[j].Dist())
                    {
                        if(M1_wh[i].Index()==0)
                            {
                            result = M1_wh[i].Time() + M1[j].Time();
                            M1[j+1].Time() = M1[j+1].Time() + result;
                            M1_wh[i].Dist() = M1_wh[i].Dist() - M1[j].Dist();
                            g++;
                            }
                        else
                            {
                            result = M1[j].Time();
                            M1[j+1].Time() = M1[j+1].Time() + result;
                            M1_wh[i].Dist() = M1_wh[i].Dist() - M1[j].Dist();
                            g++;
                        }
                        for(int t=j+1; t<n; t++)
                        {
                            if(M1_wh[i].Dist()>M1[t].Dist())
                            {
                            result = M1[t].Time() + M1[t+1].Time();
                            M1[t+1].Time() = result;
                            M1_wh[i].Dist() = M1_wh[i].Dist() - M1[j].Dist();
                            g++;
                            }
                            else
                            {
                                t=n;
                            }
                        }
                    }
                    if(M1_wh[i].Dist()<M1[j].Dist())
                    {
                        if(M1_wh[i].Dist() > 0)
                        {
                        if(i==0 && j==0)
                        {
                            result = M1_wh[i].Time() + M1[j].Time() + M1_wh[i+1].Time();
                            M1[j+1].Time() = M1[j+1].Time() + result;
                            diff = M1[j].Dist() - M1_wh[i].Dist();
                            M1_wh[i+1].Dist() = M1_wh[i+1].Dist() - diff;
                            M1_wh[i+1].Index() = 1; 
                            g++;
                        }
                        else
                        {
                            result = M1[j].Time() + M1_wh[i+1].Time();
                            M1[j+1].Time() = M1[j+1].Time() + result;
                            diff = M1[j].Dist() - M1_wh[i].Dist();
                            M1_wh[i+1].Dist() = M1_wh[i+1].Dist() - diff;
                            M1_wh[i+1].Index() = 1;
                            g++;
                        }

                    }
                    }
                }
            }

        }
    /*
    for(int i=0; i<n; i++)
    {
        cout << M1[i].Time() << " " <<M1[i].Dist() <<" "<< M1[i].Index() << " " << M1[i].Deadline() << endl;
    }
            cout << endl;
            */

else{
        for(int i=0; i<k+1; i++)
        {
              Item tmp = M1[i];
              M1[i] = M1[k];
              M1[k] = tmp;
              permutation(k-1, n, M1, M1_wh); 
              tmp = M1[i]; 
              M1[i] = M1[k];
              M1[k] = tmp;
        }
        }
}
int main()
{
    vector<Item> M1; 
    vector<Item> M1_wh; 
    vector<Item> M2; 
    int a=0,b=0,c=0,d=0,n=0, k=0;
    int x=0,y=0,z=0,v=0, T=0;
    //Item result;
    //Item diff;
    ifstream filee;
    filee.open("tasks.txt", ios::in);
    filee>>n;
    k = n-1;
    for(int i=0; i<n; i++)
    {
        filee >> a >> b >> c >> d;
        M1.push_back(Item(a,b,c,d));
    }
    filee.close();
    cout << " " << endl;
    cin >> x;
    cout << " " << endl;
    cin >> T;
   for(int i=0; i<n; i++)
   {
       z,v=0;
       y = rand() % T + 1;
       M1_wh.push_back(Item(x,y,z,v));
   }

permutation(k, n, M1, M1_wh);
return 0;
}

Last edited on
Time() etc. methods returns temporary copy of internal value which cannot be assigned to. Either return by reference or assign to internal variables directly.
M1[i+1].time instead of M1[i+1].Time()
solved ;>
Topic archived. No new replies allowed.