samedi 27 juin 2020

Method error "not all control paths return a value" and Method not returning a value

I'm getting the following errors with my code:

PoliceOfficer.cpp(39): warning C4715: 'PoliceOfficer::patrol': not all control paths return a value

ParkingTicket.cpp(54): error C4716: 'GetCarInfo': must return a value

I've commented out most of the PoliceOfficer::patrol so it is easier to troubleshoot. Not sure what I'm doing wrong. I've included all my files just in case something helps

        Car.h


    #pragma once
    #ifndef CAR_H
    #define CAR_H
    #include <string>
    
    using namespace std;
    
    // Car class contains the information about a car
    
    class Car
    {
    private:
        string make, model, color, licenseNumber;
    
    public:
        // Default Constructor
        Car();
    
        // Accessors
        string getMake();
        string getModel();
        string getColor();
        string getLicense();
    
        // Mutators
        void setMake(string);
        void setModel(string);
        void setColor(string);
        void setLicense(string);
    
        // Overload << operator
        friend ostream& operator << (ostream& strm, const Car& obj);
    
    };
    
    
    
    #endif // CAR_H
        Car.cpp


    #include "Car.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Default Constructor
    Car::Car()
    {
        make = "None Set";
        model = "None Set";
        color = "None Set";
        licenseNumber = "None Set";
    }
    
    // Accessors
    string Car::getMake()
    {
        cout << "Make: " << make;
        return make;
    
    }
    string Car::getModel()
    {
        cout << "Model: " << model;
        return model;
    }
    string Car::getColor()
    {
        cout << "Color: " << color;
        return color;
    }
    string Car::getLicense()
    {
        cout << "License: " << licenseNumber;
        return licenseNumber;
    }
    
    // Mutators
    void Car::setMake(string ma)
    {
        make = ma;
    }
    void Car::setModel(string mo)
    {
        model = mo;
    }
    void Car::setColor(string c)
    {
        color = c;
    }
    void Car::setLicense(string l)
    {
        licenseNumber = l;
    }
    
    // Overload << operator
    // Author note: I'm not sure what I am supposed to be overloading
    // this function with. 
    ostream& operator << (ostream& strm, const Car& obj)
    {
        return  strm;
    }
        ParkedCar.h


    #pragma once
    #ifndef PARKEDCAR_H
    #define PARKEDCAR_H
    #include <iostream>
    #include <string>
    #include "Car.h"
    
    using namespace std;
    
    // ParkedCar class simulates a parked car. Knows which type of car is parked and 
    // the number of minutes that the car has been parked
    
    class ParkedCar
    {
    private:
        // Private Members
    //  class Car;
        Car carObject;
        string make;
        string model;
        string color;
        string licenseNumber;
        int minutesParked;
    
    public:
        // Constructors
        ParkedCar();
        ParkedCar(string ma, string mo, string c, string l, int mp);
    
        // Copy Constructor
        ParkedCar(ParkedCar& obj);
    
        // Accessors
        int getMinutesParked() const;
        string getMake();
        string getModel();
        string getColor();
        string getLicense();
    
        // Mutators
        int setMinutesParked(int);
        void setMake(string ma);
        void setModel(string mo);
        void setColor(string c);
        void setLicense(string l);
    
    
    };
    
    #endif // PARKEDCAR_H

        ParkedCar.cpp


    #include "ParkedCar.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    ParkedCar::ParkedCar() // Default Constructor
    {
        minutesParked = 0;  
    }
    
    // make, model, color, licenseNumber;
    ParkedCar::ParkedCar(string ma, string mo, string c, string l, int mp) // Constructor with 5 parameters
    {
        carObject.setMake(ma);
        carObject.setModel(mo);
        carObject.setColor(c);
        carObject.setLicense(l);
        minutesParked = mp;
    }
    
    // Copy Constructor
    ParkedCar::ParkedCar(ParkedCar& obj)
    {
        carObject.setMake(obj.make);
        carObject.setModel(obj.model);
        carObject.setColor(obj.color);
        carObject.setLicense(obj.licenseNumber);
        minutesParked = obj.minutesParked;
    }
    
    // Accessors    
    string ParkedCar::getMake()
    {
        return make;
    }
    
    string ParkedCar::getModel()
    {
        return model;
    }
    
    string ParkedCar::getColor()
    {
        return color;
    }
    
    string ParkedCar::getLicense()
    {
        return licenseNumber;
    }
    
    int ParkedCar::getMinutesParked() const
    {
        return minutesParked;
    }
    
    // Mutators
    void ParkedCar::setMake(string ma)
    {
        make = ma;
    }
    
    void ParkedCar::setModel(string mo)
    {
        model = mo;
    }
    
    void ParkedCar::setColor(string c)
    {
        color = c;
    }
    
    void ParkedCar::setLicense(string l)
    {
        licenseNumber = l;
    }
    
    int ParkedCar::setMinutesParked(int mp)
    {
        minutesParked = mp;
        return 0;
    }
        ParkingMeter.h


    #pragma once
    #ifndef PARKINGMETER_H
    #define PARKINGMETER_H
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    class ParkingMeter
    {
    private:
        // Private Members
        int minutes; // Minutes purchased for parking
    
    public:
        // Constructors
        ParkingMeter();
        ParkingMeter(int);
    
        // Accessors
        int getMinutes();
    
        // Mutators
        int setMinutes(int);
    };
    
    #endif // PARKINGMETER_H
        ParkingMeter.cpp


    #include "ParkingMeter.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Default Constructor
    ParkingMeter::ParkingMeter()
    {
        minutes = 0;
    }
    
    // Constructor with 1 parameter
    ParkingMeter::ParkingMeter(int i)
    {
        minutes = i;
    }
    
    // Accessor
    int ParkingMeter::getMinutes()
    {
        return minutes;
    }
    
    // Mutator
    int ParkingMeter::setMinutes(int m)
    {
        minutes = m;
        return 0;
    }
    
    // Overload << operator
    const ostream& operator << (ostream& strm, const ParkingMeter& obj)
    {
        return strm;
    }
        PoliceOfficer.h


    #include "ParkingMeter.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Default Constructor
    ParkingMeter::ParkingMeter()
    {
        minutes = 0;
    }
    
    // Constructor with 1 parameter
    ParkingMeter::ParkingMeter(int i)
    {
        minutes = i;
    }
    
    // Accessor
    int ParkingMeter::getMinutes()
    {
        return minutes;
    }
    
    // Mutator
    int ParkingMeter::setMinutes(int m)
    {
        minutes = m;
        return 0;
    }
    
    // Overload << operator
    const ostream& operator << (ostream& strm, const ParkingMeter& obj)
    {
        return strm;
    }
        PoliceOfficer.cpp


    #include "PoliceOfficer.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    //Default Constructor
    PoliceOfficer::PoliceOfficer()
    {
        ParkingTicket* ticketPtr = nullptr;
        officerName = " ";
        badgeNumber = " ";
    }
    
    // Constructor with 2 elements
    PoliceOfficer::PoliceOfficer(string n, string b)
    {
        officerName = n;
        badgeNumber = b;
    }
    
    ParkingTicket *PoliceOfficer::patrol(ParkedCar car, ParkingMeter meter)
    {
        parkedMinutes = car.getMinutesParked();
        cout << "Parked Minutes: " << parkedMinutes << endl;
        meterMinutes = meter.getMinutes();
        cout << "Meter Minutes: " << meterMinutes << endl;
        minutes = parkedMinutes - meterMinutes;
        cout << "Minutes: " << minutes << endl;
        if (minutes <= 0)
        {
            return nullptr;
        }
        else
        {
            ParkingTicket ticket(car, minutes);
        };
        
    }
    // Accessors
    string PoliceOfficer::getOfficerName()
    {
        return officerName;
    }
    string PoliceOfficer::getBadgeNumber()
    {
        return badgeNumber;
    }
    
    // Mutators
    string PoliceOfficer::setOfficerName(string n)
    {
        officerName = n;
        return 0;
    }
    string PoliceOfficer::setBadgeNumber(string b)
    {
        badgeNumber = b;
        return 0;
    }
    
    // Overload << operator
    const ostream& operator << (ostream& out, const PoliceOfficer& obj)
    {   
        out << "Officer: " << &PoliceOfficer::getOfficerName;
        out << " (badge #" << &PoliceOfficer::getBadgeNumber;
        out << ")" << endl;
        return out;
    }
        ParkingTicket.h


    #pragma once
    #ifndef PARKINGTICKET_H
    #define PARKINGTICKET_H
    #include <iostream>
    #include <string>
    #include "ParkedCar.h"
    #include "ParkingMeter.h"
    #include "Car.h"
    
    using namespace std;
    
    // ParkingTicket class simulates a parking ticket
    // Reports the car information for illegally parked car
    // Reports the fine which is $25 for the first hour or part
    // of an hour that the car is illegally parked, plus $10
    // for every additional hour or part of an hour
    
    class ParkingTicket
    {
    
    private:
        // Private Members
    //  class ParkedCar;
        double fine; // The calculated parking fine
        double* fineptr = &fine; // fine pointer
        int minutes; // The minutes illegally parked
        const int firstHour = 25; // first hour fine
        const int subHours = 10; // subsiquent hour fine
    
        // Car Info
        string make, model, color, licenseNumber;
    
    
    public:
        // Constructors
        ParkingTicket();
        ParkingTicket(ParkedCar car, int meter);
    
        // Accessors
        double getFine();
        int getMinutes();
    
        // Mutators
        double setFine(double);
        int setMinutes(int);
    
        // Method
        double fineCalc(); // calculate the fine
        string GetCarInfo();
    
    //  friend const ostream& operator << (ostream&, const ParkingTicket&);
    };
    
    const ostream& operator << (ostream&, const ParkingTicket&);
    
    #endif // PARKINGTICKET_H
        ParkingTicket.cpp


    #include "ParkingTicket.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    ParkingTicket::ParkingTicket() // Default Constructor
    {
        double fine = 0;
        double* fineptr = &fine;
        int minutes = 0;
        const int firstHour = 25;
        const int subHours = 10;
    }
    
    // Constructor with 2 parameters (ParkedCar and an integer)
    ParkingTicket::ParkingTicket(ParkedCar car, int min) 
    {
        minutes = min; // illegally parked minutes
        cout << "Minutes: " << endl;
        ParkingTicket::fineCalc(); // calculate fine
        cout << fine;
    }
    
    // Accessors
    double ParkingTicket::getFine()
    {
        return fine;
    }
    
    int ParkingTicket::getMinutes()
    {
        return minutes;
    }
    
    // Mutators
    double ParkingTicket::setFine(double f)
    {
        fine = f;
        return 0;
    }
    
    int ParkingTicket::setMinutes(int m)
    {
        minutes = m;
        return 0;
    }
    
    string GetCarInfo()
    {
        &Car::getMake;
        //&Car::getModel;
        //&Car::getColor;
        //&Car::getLicense;
    }
    
    // Car information, Time on the meter
    double ParkingTicket::fineCalc()
    {
        fine = 25;
        minutes -= 60;
        while (minutes >= 60)
        {
            fine += subHours;
            minutes -= 60;
        }
        if (minutes >= 0)
        {
            fine += subHours;       
        }
        return fine;
    }
    
    // Overload << operator
    const ostream& operator << (ostream& out, const ParkingTicket& obj)
    {
        out << &ParkingTicket::GetCarInfo;
        return out;
    }
        main.cpp


    #include <iostream>
    #include <string>
    #include "ParkedCar.h"
    #include "ParkingMeter.h"
    #include "ParkingTicket.h"
    #include "PoliceOfficer.h"
    
    using namespace std;
    
    int main()
    {
    
        ParkingTicket *ticket = nullptr;    
    
        ParkedCar car("Volkswagen", "1972", "Red", "147RHZM", 125);
    
        ParkingMeter meter(60);
    
        PoliceOfficer officer("Joe Friday", "4788");
    
        ticket = officer.patrol(car, meter);
        if (ticket != nullptr)
        {
            cout << "we made it this far officer" << endl;
            cout << officer;
            cout << "we are at the ticket" << endl;
            cout << *ticket;
    
            delete ticket;
            ticket = nullptr;
            
        }
        else
            cout << "No crimes were committed.\n";
    
        return 0;
    }

Aucun commentaire:

Enregistrer un commentaire