I have four classes: ParkedCar, ParkingMeter, ParkingTicket, and PoliceOfficer. The PoliceOfficer class should "issue a ticket", ie create a new ParkingTicket object, if a car has been parked longer than it should be. A ParkingTicket object has, as member objects, a ParkedCar object and PoliceOfficer object (basically objects containing info for what car is getting the ticket, and the issuing officer). Below are my constructors for my ParkingTicket class, I'm trying to use the first one in creating my ParkingTicket.
From ParkingTicket class:
public ParkingTicket(ParkedCar parkedCarObj, PoliceOfficer officerObj){
this.car = new ParkedCar(parkedCarObj);
this.officer = new PoliceOfficer(officerObj);
}
//Copy constructor, makes new instance a copy of object passed as argument
public ParkingTicket(ParkingTicket obj2){
this.car = new ParkedCar(obj2.car);
this.officer = new PoliceOfficer(obj2.officer);
}
My method for issuing a ticket in the PoliceOfficer class is below. If I need to issue a ticket, I try to pass the ParkedObject and the self PoliceOfficer object (by using this) to the ParkingTicket constructor, as below.
From PoliceOfficer class:
public boolean issueTicket(ParkedCar car, ParkingMeter meter){
boolean expired = false;
if (car.getMinsParked() > meter.getMinsPurchased()){
expired = true;
ParkingTicket ticket = new ParkingTicket(car, this); //Compiler error
}
return expired;
}
However, the line where I instantiate the new ParkingTicket throws a compiler error. The message is:
constructor ParkingTicket in class ParkingTicket cannot be applied to given types;
required: ParkedCar
found: ParkedCar,PoliceOfficer
reason: actual and formal argument lists differ in length
I'm very confused because it should be invoking the first overloaded constructor, which takes a ParkedCar and a PoliceOfficer as arguments. I'm not certain why it's saying it should only take one argument of ParkedCar. If I only pass a ParkedCar object as an argument, it compiles fine, but I know this is incorrect because I've not passed the needed PoliceOfficer info to the ParkingTicket object.
Any ideas? Appreciate any help.
.classfiles are up to date? the error message says it requires aParkedCarparameter only. The single argument constructor you show had a different type so it's not an overload issue.ParkingTicket, you pass instances ofParkedCarandPoliceOfficeras parameters. Why are you creating new instances ofParkedCarandPoliceOfficerwhen assigning them tothis.carandthis.officer, with the passed instances? It sounds like "assign a car to this by creating a car with an identical car". Other than that, try do as dkatzel is suggesting, try rebuild your project.