Thread: accessing varible in a c++ class

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    12

    Question accessing varible in a c++ class

    I have a program that is trying to access a varible from a class in a porgram. For example

    the varible customer is my Account.h file and in my Bank.h I have another varible called Account savings, they are both arrays.

    In my Bank.cpp file I have to take the info from Account savings and put in the customer varible.

    the line of code looks like this

    savings[pos].customer[j]=c[m];

    and I get the error something like
    can't access the private member varible

    What is the problem, keep in mind that I have added the includes for Account.h and Bank.h in Bank.cpp

    Thanks

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    A couple of ideas....

    Make sure that the member variable is public.

    I'm not going to attempt the syntax off the top of my head, but... Somtimes I've had to pass a pointer-to-an-object into a member function in another class. Make sure all of your variables are in-scope. All the variables that your function uses have to be passed-into the function (directly, via pointers, or via references).

    And, it's usually considered better to make the member variables private, and provide a public accessor function, which is a public member function that returns the particular variable, rather than directly accessing the member variable.
    Last edited by DougDbug; 03-10-2004 at 03:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  4. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM