Access private member variable

I've created a class called Voter with a private member variable ID, also I have a variable in my main function to be ID as well. I'm trying to compare the two ID's but when I do so:

if (ID == V.ID)

I get the error - 'std::string Voter::ID' is private within this context.
I know that because it's private I can't access it, but how do I?
You create a public member function that returns the string.
Last edited on
Yea I have done that now.

1
2
3
4
string Voter::getID()
{
return ID;
}


but it doesn't seem to work. I don't get any error but when I enter an ID even if they are the same it doesn't seem to pick i t up.

For example the correct ID is: 19810102009
and if I enter that ID it should print
CORRECT!
to the screen, but it aint..
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
28
29
30
31
32
#include <iostream>
#include <string>

struct Voter
{
    Voter(std::string ID) : id(ID)
    {
    }

    std::string ID()
    {
        return id;
    }

private:
    std::string id;
};

int main()
{
    Voter v ("19810102009");
    std::string Id;
    std::cin >> Id;
    if(v.ID() == Id)
    {
        std::cout << "CORRECT!" << std::endl;
    }
    else
    {
        std::cout << "Invalid username or password." << std::endl;
    }
}
http://ideone.com/eO3Qvq "CORRECT!"
http://ideone.com/BNYKSx "Invalid username or password."
Topic archived. No new replies allowed.