Accessor Function

This is a discussion on Accessor Function within the C++ Programming forums, part of the General Programming Boards category; I have a simple program that asks a user for personal info (age, phone ...) and stores this info in ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Accessor Function

    I have a simple program that asks a user for personal info (age, phone ...) and stores this info in the private memberspace.
    Then the info is displayed and asks the user if this is correct and if to make changes, if so to what.

    Part of my private looks like this

    Code:
    double set_new_age ();
    In my main, I have this code

    Code:
    double new_age;
    cin >> new_age;
    Mike.set_new_age ();
    and a function :

    Code:
    void Person::set_new_age (double new_age_p  ) 
        {
          age = new_age_p ; 
        }
    I am getting 2 errors:

    d:\docume~1\suchy\desktop\info~1.cpp:208: prototype for `void Person::set_new_age(double)' does not match any in class `Person'

    d:\docume~1\suchy\desktop\info~1.cpp:19: candidate is: double Person::set_new_age()
    What is the problem, I can't figure this out .

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Your 'privates' should look like

    Code:
    double set_new_age(double);
    And you should 'sleep with your mom' like

    Code:
    Mike.set_new_age(new_age);
    You have to pass your 'age' to your 'mom'

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's the declaration for member function set_new_age.
    Code:
    >double set_new_age ();
    Now here's the definition:
    Code:
    >void Person::set_new_age (double new_age_p  )
    See the difference?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Your 'privates' should look like
    >> double set_new_age(double);

    That's not how I would do it. There doesn't appear to be a reason to return anything from a set function.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Maybe the previous value it contained. (Floundering)

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Also, this is generally not considered an accessor; it's a mutator.

    An accessor would be double Person::get_age() const;
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21