Access to public member in a Class

Hi,

Can someone help me with this,

i am trying to access a variable in the public domain of a class.

1
2
3
4
5
6
7
8
class train {
			public: 
				
				train(int X,int Y,int direction, int length, int displacement, int passengerlimit);
				int move(void);
				void reverse(void);

}train1

I am trying to access the displacement value but cant seem to as its in brackets.

train train1(0,1,1,60,4,MaxPassengers);

From the line above, I want to change the value of 4 (displacement) but it doesnt want to let me!

Any ideas?

Thanks
Well displacement is not a variable or a member but an argument.
The arguments passes to the train constructor are usually used in the initialization of some member values however you haven't got an members to initialize. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class train {
   // private by defualt
   int x;
   int y;
   int Direction;
   // etc...

public:
   train(int X,int Y,int direction, int length, int displacement, int passengerlimit) {
         x = X;
         y = Y;
         Direction = direction;
   }
   int move(void);
   void reverse(void);
} train1;


EDIT* I should note however that the method used in the example above is not the best way to utilize the contrsuctor. Refer to this article for better use: http://www.cplusplus.com/forum/articles/17820/

Last edited on
heres the full class if it helps.


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
class train {
			public: 
				
				train(int X,int Y,int direction, int length, int displacement, int passengerlimit);
				int move(void);
				void reverse(void);
				void append(passenger value);
				passenger take(void);
				int length(void);
				void color(GColour color);
				BufferGeneric <passenger> *passengers; 



			private:
				int train_index;
				int ypos;
				int disp;
				int len;
				int front;
				int back;
				GColour tcolor;
				int disparg;
				int lenarg;
				void drawTrainSet ( );

}; 





And this is part of the constructor:

1
2
3
4
5
6
7
8
9
10
11
 train::train(int X,int Y,int direction, int length, int displacement,int passengerlimit)
{

	ypos = Y;
	front = length;   //X;



	disparg = displacement;
	lenarg = length;
	tcolor = GwinColourNames::BLACK;  // default train colour 



And we have train train1(0,1,1,60,4,MaxPassengers);

With the value 4 (which is displacement) I need to change (later on in the code)

Thanks
How do you expect to access the public member disparg when it happens to be private? That's an interesting contradiction you have there.
int displacement is a constructor argument. It's not a member of your class. The member against which it is assigned, disparg, is a private member and cannot be accessed outside the class.
You need to create a 'set_disparg()' fucntion that does have access to private members.

EDIT* Line 5 of your constructor: if 'front' is being set to 'length', what is the need for the 'int Xi parameter?
Last edited on
Topic archived. No new replies allowed.