Changing object values in different function

I have an object that is created from another void that loads it's values from a file.

Now I'm trying to edit these values using another function, I've tried using a reference type but it doesn't work it still stays as the same value.

1
2
3
4
5
6
7
cout << square_objects[5]->rent; //is 50

//in this class function I want too be able too change the value of square_object[5]->rent, or anything to do with the square object.
engine->land_function(0,0);


cout << square_objects[5]->rent; //is still 50 


How would I go around doing this?
Could you post the code that you have tried?
You can change an object with a function that takes a pointer or reference to it.
vector<CSquare*> square_objects;
square(square_objects);

square is a function that loads the object values from a textfile.

 
square_objects[1]->name; // would give the name of the second square. 


Now, I've got a new function called engine, I was wondering how I can manipulate these values in that function and still be able to return too this in the main:

 
square_objects[1]->name; // would give the name of the second square. //should be edited, but it's not working. 


I can't get refereing too work, it just keeps throwing out loads of errors.

another example:

1
2
3
4
5
6
7
cout << square_objects[5]->rent;

//testing to try and alter the iterators in this run main
engine->land_function(0,0);

cout << square_objects[5]->rent;
That's not what isnork meant..

Anyway, example code:
1
2
3
4
5
6
7
8
9
10
void func( Type& obj ){
   //do things with obj
}

int main(){
   Type object;
   func(object);
   //now object might be different
   return 0;
}

If you replace "Type" with "std::vector<CSquare*>" all should be well.
Ok, I've done this in the class:

1
2
3
4
5
6
7
class blah
{
void land_function(float,float, vector<CSquare*>);
}

//within the class
void CGame::land_function(float land_line, float player_no, vector<CSquare*>& square){}


And I've done this in the function:

 
engine->land_function(0,0, square_object);


still getting loads of errors, I've tried using pointers and references all over the place, but still nothing. :-*(
Ok I forgot too put a pointer in <CSquare*>

Seems too be working but, when I return the value after the function the value still hasn't changed, I,e:

1
2
3
4
5
6
void CGame::land_function(float land_line, float player_no, vector<CSquare*>& square)
{
	square[1]->rent = square[1]->rent + 500;
	//changing the void run interators

}


Then I cout the square in the main it just shows the original value.
The problem in your previous post was that you didn't put a & on line 3. There is nothing I can find in your last post. There's just too little code. Post how you declare the function, how you define it and how you call it.
Basically, the object is made like this:
1
2
3
4
void square(vector <CSquare*> &list_squares)
{
//stuff is done in here to pushback too the object
}


Then it's called in the main, at this point I can call all the objects values (squares ie, name, value)

1
2
vector<CSquare*> square_objects;
	square(square_objects);


Now I want to be able to use a function to edit the values of the squares[number], but this function is within a manager class like so:

1
2
3
4
5
6
7
8
9
10
class CManager_Class
{
public:
	void do_stuff(float,float, vector<CSquare*> &);
};

void CManager_Class::do_stuff(float land_line, float player_no, vector<CSquare*>& square)
{
	square[1]->rent = square[1]->rent + 500;
}


Once this is done I want the values of Square too change outside this function.

For some reason this isn't working at all, there's isn't any errors but the values don't change outside the square.
Here,
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
#include <iostream>
#include <vector>

using std::vector;

struct CSquare{
	int rent;

	CSquare(int i) : rent(i) {}
};

void square(vector <CSquare*> &list_squares){
	list_squares.push_back(0);
	list_squares.push_back(new CSquare(5));
}

class CManager_Class{
public:
	void do_stuff(float,float, vector<CSquare*> &);
};

void CManager_Class::do_stuff(float, float, vector<CSquare*>& square){
	square[1]->rent = square[1]->rent + 500;
}

int main(){
	vector<CSquare*> square_objects;
	square(square_objects);

	std::cout << square_objects[1]->rent << '\n';

	CManager_Class manager;
	manager.do_stuff(0, 0, square_objects);

	std::cout << square_objects[1]->rent << '\n';

	std::cin.get();
	return 0;
}

That code can be made into something working without changing any original parts. Thus there is nothing wrong with it. Look for the problem elsewhere.
The code is working it's just not changing the value of square, what you have above is exactly what I've got shaven down.
just post all you've got.. use pastebin if there's a lot..
I know what I was doing, I forgot to put square[//the number] ;(

Thanks for you help all!
Topic archived. No new replies allowed.