-2

I've been reading a book called c++ for dummies (7th edition) and I'm currently going through the chapter about classes. I decided to try and write my own program using classes and it looked easier than I found it. I'm having this persistent problem ('expected unqualified id before "." token) that hasn't gone away with the fixes I've tried. It seems this question is program specific so I'm fine with it getting shutdown for only being helpful to me. Here's the code:

class savings {
public:
    void Set_Pin(double pin) {
      savings.pin = pin; // this is where the error is
    }
private:
    int pin;
    int username;
};

how do I fix the error? thanks

RvdK
  • 19,580
  • 4
  • 64
  • 107
Ciaran Walker
  • 73
  • 2
  • 7
  • you have to use the http://en.cppreference.com/w/cpp/language/this – Rohith R Oct 06 '16 at 08:52
  • it's a bit weird to have your arg having the same name as a member here, if you changed your arg name to `p` then you could just do `pin = p;` inside your function body – EdChum Oct 06 '16 at 08:53
  • @EdChum I would strongly advise not to use meaningless names. this.pin is sufficient. – RvdK Oct 06 '16 at 08:54
  • Yep, _savings_ is a class that will be at some point made into an object in memory (instantiated). You then can access that particular instance's internal members through the _this_ pointer. So in this case `this->pin` . – Steeve Oct 06 '16 at 08:54
  • I should probably change the variable names then... – Ciaran Walker Oct 06 '16 at 08:57
  • @RvdK that's true, personally I'd name the member variable `mPin` or `pin_` and just assign using `mPin = pin;` or `pin_ = pin;` to make it simpler, I don't use the convention of `this->member = arg;` as it's a bit verbose to me – EdChum Oct 06 '16 at 09:03

3 Answers3

1

You need to use this i.e.

change

 savings.pin = pin;

to

 this->pin = pin;
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

It should be:

void Set_Pin(double pin) {
  this->pin = pin;
}

Also this should not be needed, but due the same naming with the function parameter it would be wise to use it indeed, more clear for the user.

Also regarding style. I would use start class name with a capital letter.

RvdK
  • 19,580
  • 4
  • 64
  • 107
0

"pin" is a member of your class so you don't need to specify "saving.pin" since from the point of view of the class "pin" directly refers to its member.

Also i suggest that you name the variable of Set_Pin differently such as :

void Set_Pin(double aPin) {
    pin = aPin; 
}
A.KYROU
  • 23
  • 3