Problem with Assigning to Vector!!

Can someone explain to me how I would go about assigning the vector<float> value in the example below a new value AFTER the program runs?

When I resize the vector, the 2.0 value is correctly set; but when I try to reset it to 4.0 in the main() function, it doesn't change the value and just prints 2.0.

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
#include <iostream>
#include <vector>

using namespace std;


class grid;

class cell{
public:
  friend class grid;

  cell operator[](int index) 
    { 
       if ((index < 0) || (index > 3))
	{ _index = -1; return *this; }
	  _index = index; 
	  return *this; 
	} 

	void SetValue(float);
	float GetValue();

private:
  int _index;
  vector<float> value;
};

void cell::SetValue(float val)
{ value[_index] = val; }

float cell::GetValue()
{ return value[_index]; }

class grid{
public:
  cell Cell;
  grid()
  {
    Cell.value.resize(4,2.0);
  };
};

int main()
{

grid gridBoard;
gridBoard.Cell[3].SetValue(4.0);
cout << gridBoard.Cell[3].GetValue() << endl;


cout << endl;
system("pause");
return 0;
}
This code is really confusing.

The culprit here is that your [] operator is returning a copy of the cell, and not the actual cell in the grid. So when you SetValue, you're actually setting the value in a copy... leaving gridBoard's copy unchanged.

You can fix this by having your [] operator return by reference.





But... again... your organization here is very strange. For starters, 'cell' is inappropriately named, since it's actually 4 different cells and not actually a single individual cell as the name implies. Futhermore, there's no real reason to have two classes like this. And there isn't even really a reason to use a vector here.

And your state-based approach to keep track of the current index is very, very weird and would allow for some very convoluted code.

This can all be greatly simplified:

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
#include <iostream>
#include <vector>

using namespace std;

class grid
{
public:
    grid()
    {
        values.resize(4,2.0f);
    }

    float& operator [] (int index)
    {
        return values[index];
    }

private:
    vector<float> values;
};

int main()
{
    grid gridBoard;
    gridBoard[3] = 4.0f;
    cout << gridBoard[3] << endl;

    system("pause");
    return 0;
}



Or .... if you want explicit get/set functions instead of array-style access:

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
#include <iostream>
#include <vector>

using namespace std;

class grid
{
public:
    grid()
    {
        values.resize(4,2.0f);
    }

    float getCell(int index)
    {
        return values[index];
    }

    void setCell(int index, float val)
    {
        values[index] = val;
    }

private:
    vector<float> values;
};

int main()
{
    grid gridBoard;
    gridBoard.setCell(3, 4.0f);
    cout << gridBoard.getCell(3) << endl;

    system("pause");
    return 0;
}
Topic archived. No new replies allowed.