Thread: accessing setter methods

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    accessing setter methods

    How to i access setter from within a setter?

    i know that getter methods can be access like
    Code:
    getAccounts().getAccountNumber(); // for example
    but for setters how do i do it? this doesnt seem to work though
    Code:
    setAccounts().setAccountNumber(accountNumber);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    59
    it gave me an error
    no matching function for call clsLocalStudent::setAccounts()'
    candidate expects 1 argument, 0 provided

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you want to use getAccounts() to get the account to set the number for? How do you know which thing you're setting?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. What does the error message mean to you?

    EDIT:
    Well, tabstop is cutting to the chase, so I guess I might as well do so as well:

    Quote Originally Posted by Alexius Lim
    How to i access setter from within a setter?
    You call the function. The problem is, you need a concrete example. Let's consider your getter example first:
    Quote Originally Posted by Alexius Lim
    i know that getter methods can be access like
    Code:
    getAccounts().getAccountNumber(); // for example
    The above code does not access a getter from within a getter. Rather, what it does is function chaining: getAccounts() returns a value, presumably of some class type, then you called the getAccountNumber() member function with that object returned.

    Quote Originally Posted by Alexius Lim
    but for setters how do i do it? this doesnt seem to work though
    Code:
    setAccounts().setAccountNumber(accountNumber);
    The above code is similiar to your previous example. The problem is that setAccounts apparently takes an argument that you did not provide. But what are you trying to do? You're trying to call setAccounts to set accounts? What does that mean? Why do you then want to call setAccountNumber with what it returns?
    Last edited by laserlight; 11-21-2013 at 09:08 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    59
    Erm. Student has accounts. And within these accounts, it has other accounts information such as the name amount etc. I'm trying to set those members.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Alexius Lim
    Erm. Student has accounts. And within these accounts, it has other accounts information such as the name amount etc. I'm trying to set those members.
    Okay. So what I expect is something like this:
    Code:
    Student student;
    // ...
    Account& account = student.getAccounts(some_account_identifier);
    account.setAccountNumber(some_value);
    std::cout << account.getAccountNumber() << std::endl;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2013
    Posts
    59
    im doing it like this.

    in student class
    Code:
    void clsStudent::setAccounts(clsAccount *argAccounts)
    {
        accounts = argAccounts;
    }
    
    
    clsAccount* clsStudent::getAccounts(void) const
    {
        return accounts;
    }
    in account class
    Code:
    void clsAccount::setAccountNumber(string accNo)
    {
        accountNumber = accNo;
    }
    
    
    string clsAccount::getAccountNumber(void) const
    {
        return accountNumber;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing public methods of class members
    By mkone in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2012, 01:20 PM
  2. Accessing Member Methods inside a Vector
    By xmltorrent in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2008, 12:02 PM
  3. Object array. Probelm accessing methods
    By Bean in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2004, 11:22 AM
  4. help with calling this setter
    By mufugger in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 12:46 AM
  5. Replies: 8
    Last Post: 07-27-2003, 01:52 PM