0

Okay, so this is a parking ticket application... I'm sure you've all seen/done them before. This one is for C++ and I'm having trouble with getting my class-contained methods to access private members of a friend class. I know that this is completely a syntax error, but I can't seem to find an example that makes sense to me on what the syntax actually is.

This is (one of) the lines that I get errors on. They're all the same error,

    cout<<"Please visit your local police department issued by "<<policeOfficer.name<<" badge number "<<policeOfficer.badgeNumber <<" to pay your $"<<fine<<" fine.";

And here is the beginning of the policeOfficer class, in case I haven't appropriately declared friend status between the classes.

class policeOfficer
{
private:
string name, badgeNumber;
public:
friend class parkedCar;
friend class parkingMeter;
friend class parkingTicket;

There's a couple of errors. "error C2027: use of undefined type" "left of '.badgeNumber' must have class/struct/union" "type name not allowed" "identifier createTicket is undefined"

But they all center around that single line of code, plus a few others that I have written the same exact way.

Sorry if I've missed anything necessary, I'd be more than happy to provide more information if I did.

Heather T
  • 323
  • 1
  • 10
  • 20
  • 1
    What error? I see no error. – chris May 15 '13 at 03:32
  • 3
    Probably you meant `friend class parkedCar;`, etc. – Jesse Good May 15 '13 at 03:35
  • Adding the "class" keyword got rid of a few errors - I actually had it in my other friend declarations just apparently missed it there. :/ Thanks – Heather T May 15 '13 at 03:39
  • Also, `policeOfficer` is the **name** of a class, not an object that exists in memory. You need to instantiate an object first, i.e. `policeOfficer p; p.name;`. Instead of guessing the syntax, I recommend [reading some books on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that teach you this. – Jesse Good May 15 '13 at 03:42
  • I've read the chapter in my C++ book twice through before asking here, actually. I can't find a specific example for accessing variables and not methods. – Heather T May 15 '13 at 03:44

1 Answers1

1

policeOfficer is name of a class and you are trying to access a private member variable using class name so it is giving error. Create an object of policeOfficer obj and then access the variable obj.badgenumber.

Daemon
  • 1,575
  • 1
  • 17
  • 37
  • Forgot to set up the access from the main. No wonder they wouldn't interact! Thanks. I've got the errors whittled down considerably now. – Heather T May 15 '13 at 04:16