[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

ParkingTicket class not playing nice with others

Hi again experts. I have now added a ParkingTicket class to my project to write the poor car who goes overtime a hefty ticket. However, When I try to compile the entire sucker, i get about 4 errors, telling me that ParkedCar is not a type inside my ParkingTicket.h file. The parking ticket should use the computeFine method when called and display all the information about the car, the fine amount, and the officer's badge number and name. The officer is what I have to have done by next week, so for now I have a static name and number.

 Thank you experts, I am new to object oriented programming in C++ and am a bit lost as to understanding why it isn't recognizing my use of the ParkedCar class. Am I not suppose to call the class in that way?
Driver:
 
#include <iostream>
#include "ParkedCar.h"
#include "ParkingMeter.h"
#include "ParkingTicket.h"
 
using namespace std;
 
int main()
{
	ParkedCar car1;
	ParkingMeter meter1;
	meter1.setTime(100);
	car1.setMake("Ford");
	car1.setModel("Taurus");
	car1.setColor("Maroon");
	car1.setLicence(1337);
	car1.setMinutes(25);
	ParkingTicket ticket1(car1,25,"Lou",92);
	ticket1.setMinutesIllegalPark(15);
	cout << ticket1.getMinutesIllegalPark() << endl;
}	
 
ParkedCar.h
 
#ifndef PARKEDCAR_H
#define PARKEDCAR_H
 
class ParkedCar
{
	private:
		char make[30];
		char model[30];
		char color[30];
		int licenceNumber;
		int minutesParked;
	public:
		ParkedCar();
		ParkedCar(const char* make1, const char* model1, const char* color1, int licenceNumber1, int minutesParked1);
		const char* getMake();
		const char* getModel();
		const char* getColor();
		int getLicenceNumber();
		int getMinutesParked();
		void setMake(const char * make1);
		void setModel(const char* model1);
		void setColor(const char* color1);
		void setLicence(int licenceNumber1);
		void setMinutes(int minutesParked1); 
};
#endif		
 
ParkedCar.cpp
 
#include <iostream>
#include "ParkedCar.h"
 
ParkedCar::ParkedCar()
{
	make[0] = '\0';
	model[0] = '\0';
	color[0] = '\0';
	licenceNumber = 0;
	minutesParked = '\0';
}	
ParkedCar::ParkedCar(const char* make1, const char* model1, const char* color1, int licenceNumber1, int minutesParked1)
{
	licenceNumber = licenceNumber1;
	minutesParked = minutesParked1;
}
const char* ParkedCar::getMake()
{
	return make;
}
 
const char* ParkedCar::getModel()
{
	return model;
}
 
const char* ParkedCar::getColor()
{
	return color;
}
 
int ParkedCar::getLicenceNumber()
{
	return licenceNumber;
}
 
int  ParkedCar::getMinutesParked()
{
	return minutesParked;
}
void ParkedCar::setMake(const char* make1)
{
	strcpy(make,make1);
}
void ParkedCar::setModel(const char* model1)
{
	strcpy(model,model1);
}
void ParkedCar::setColor(const char* color1)
{
	strcpy(color,color1);
}
void ParkedCar::setLicence(int licenceNumber1)
{
	licenceNumber = licenceNumber1;
}
void ParkedCar::setMinutes(int minutesParked1)
{
	minutesParked = minutesParked1;
}
	
ParkingMeter.h
 
#ifndef PARKINGMETER_H
#define PARKINGMETER_H
 
class ParkingMeter
{
	private:
		int timePurchased;
	public:
		ParkingMeter();
		ParkingMeter(int timePurchased);
		void setTime(int time);
		int getTime();
		
};
#endif
 
ParkingMeter.cpp
 
#include <iostream>
#include "ParkingMeter.h"
 
ParkingMeter::ParkingMeter()
{
	timePurchased = 0;
}
ParkingMeter::ParkingMeter(int time)
{
		timePurchased = time;
}
 
int ParkingMeter::getTime()
{
	return timePurchased;
}
 
void ParkingMeter::setTime(int time)
{
	timePurchased = time;
}
 
ParkingTicket.h
 
#ifndef PARKINGTICKET_H
#define PARKINGTICKET_H
 
class ParkingTicket
{
	private:
			ParkedCar car1;
			int minutesIllegalPark;
	public: 
			ParkingTicket();
			ParkingTicket(ParkedCar,int minutes,char*,int);
			void computeFine(ParkedCar,int,char*,int);
			int getMinutesIllegalPark();
			void setMinutesIllegalPark(int minutes);
};
#endif			
 
ParkingTicket.cpp
 
#include "ParkingTicket.h"
#include <iostream>
 
ParkingTicket::ParkingTicket()
{
	minutesIllegalPark = 0;
}
ParkingTicket::ParkingTicket(ParkedCar car1,int minutesIllegalPark,char* officer,int badgeNumber)
 
	
{
		computeFine(ParkedCar car1,int minutesIllegalPark,char* officerName,int badgeNumber);
}
 
void ParkingTicket::getMinutesIllegalPark()
{
	return minutesIllegalPark;
}
 
void ParkingTicket::setMinutesIllegalPark(int minutes)
{
	minutesIllegalPark = minutes;
}
 
void ParkingTicket::computeFine(ParkedCar car1,int minutesIllegalPark,char* officerName,int badgeNumber)
{
	int fineAmount;
	
	if( minutesIllegalPark <= 15)
	{
		fineAmount = 25;
	}
	else
	{
		fineAmount = 25 + 5(minutesIllegalPark - 15)
	}
	
	cout << car1.getMake() << endl;
	cout << car1.getModel() << endl;
	cout << car1.getColor() << endl;
	cout << car1.getLicenceNumber() << endl;
	cout << fineAmount << endl;
	cout << officerName << endl;
	cout << badeNumber << endl;
}

Open in new window

0
pacman32689
Asked:
pacman32689
1 Solution
 
Anthony2000Commented:
I think if you are having a problem with the compiler not being able to find the ParkedCar class. When you define the ParkingTicket you refer to ParkedCar class, but in some cpp files the compiler will not find the definition of ParkedCar. I see in main that you include all the necessary header files. But in ParkingTicket.cpp you only include ParkingTicket.h.

The way I usually solve this, is to include the header files for any classes I use in the header file I am using the classes in.

So, in ParkingTicket.h add the include for ParkedCar.h. and then check all your other header files for any other dependencies like this.

Does this help?
0
 
Anthony2000Commented:
Your compiler will compile each .cpp separately, if while compiling it cannot find a definition for a class, it will give you the error you are seeing.
0
 
jkrCommented:
We,, a bunch if syntax problems in there, the code below compiles, see my comments:
-------------------ParkedCar.h---------------------
 
 
#ifndef PARKEDCAR_H
#define PARKEDCAR_H
 
class ParkedCar
{
        private:
                char make[30];
                char model[30];
                char color[30];
                int licenceNumber;
                int minutesParked;
        public:
                ParkedCar();
                ParkedCar(const char* make1, const char* model1, const char* color1, int licenceNumber1, int minutesParked1);
                const char* getMake();
                const char* getModel();
                const char* getColor();
                int getLicenceNumber();
                int getMinutesParked();
                void setMake(const char * make1);
                void setModel(const char* model1);
                void setColor(const char* color1);
                void setLicence(int licenceNumber1);
                void setMinutes(int minutesParked1); 
};
#endif          
 
-------------------ParkedCar.cpp---------------------
 
 
#include <iostream>
#include "ParkedCar.h"
 
ParkedCar::ParkedCar()
{
        make[0] = '\0';
        model[0] = '\0';
        color[0] = '\0';
        licenceNumber = 0;
        minutesParked = '\0';
}       
ParkedCar::ParkedCar(const char* make1, const char* model1, const char* color1, int licenceNumber1, int minutesParked1)
{
        licenceNumber = licenceNumber1;
        minutesParked = minutesParked1;
}
const char* ParkedCar::getMake()
{
        return make;
}
 
const char* ParkedCar::getModel()
{
        return model;
}
 
const char* ParkedCar::getColor()
{
        return color;
}
 
int ParkedCar::getLicenceNumber()
{
        return licenceNumber;
}
 
int  ParkedCar::getMinutesParked()
{
        return minutesParked;
}
void ParkedCar::setMake(const char* make1)
{
        strcpy(make,make1);
}
void ParkedCar::setModel(const char* model1)
{
        strcpy(model,model1);
}
void ParkedCar::setColor(const char* color1)
{
        strcpy(color,color1);
}
void ParkedCar::setLicence(int licenceNumber1)
{
        licenceNumber = licenceNumber1;
}
void ParkedCar::setMinutes(int minutesParked1)
{
        minutesParked = minutesParked1;
}
 
-----------------------ParkingMeter.h-----------------------
 
#ifndef PARKINGMETER_H
#define PARKINGMETER_H
 
class ParkingMeter
{
        private:
                int timePurchased;
        public:
                ParkingMeter();
                ParkingMeter(int timePurchased);
                void setTime(int time);
                int getTime();
                
};
#endif
 
-----------------ParkingMeter.cpp---------------------------
 
#include <iostream>
#include "ParkingMeter.h"
 
ParkingMeter::ParkingMeter()
{
        timePurchased = 0;
}
ParkingMeter::ParkingMeter(int time)
{
                timePurchased = time;
}
 
int ParkingMeter::getTime()
{
        return timePurchased;
}
 
void ParkingMeter::setTime(int time)
{
        timePurchased = time;
}
 
 
-----------------ParkingTicket.h-------------------------
 
#ifndef PARKINGTICKET_H
#define PARKINGTICKET_H
 
#include "ParkedCar.h" // <-- missing
 
class ParkingTicket
{
        private:
                        ParkedCar car1;
                        int minutesIllegalPark;
        public: 
                        ParkingTicket();
                        ParkingTicket(ParkedCar,int minutes,char*,int);
                        void computeFine(ParkedCar,int,char*,int);
                        int getMinutesIllegalPark();
                        void setMinutesIllegalPark(int minutes);
};
#endif   
 
-----------------------ParkingTicket.cpp---------------------------
 
#include "ParkingTicket.h"
#include "ParkedCar.h"
#include <iostream>
using namespace std; // missing
 
ParkingTicket::ParkingTicket()
{
        minutesIllegalPark = 0;
}
ParkingTicket::ParkingTicket(ParkedCar car1,int minutesIllegalPark,char* officer,int badgeNumber)
 
        
{
 //               computeFine(ParkedCar car1,int minutesIllegalPark,char* officerName,int badgeNumber);???
               computeFine(car1,minutesIllegalPark,officer,badgeNumber);
 
}
 
int ParkingTicket::getMinutesIllegalPark() // int, not void
{
        return minutesIllegalPark;
}
 
void ParkingTicket::setMinutesIllegalPark(int minutes)
{
        minutesIllegalPark = minutes;
}
 
void ParkingTicket::computeFine(ParkedCar car1,int minutesIllegalPark,char* officerName,int badgeNumber)
{
        int fineAmount;
        
        if( minutesIllegalPark <= 15)
        {
                fineAmount = 25;
        }
        else
        {
                fineAmount = 25 + 5 * (minutesIllegalPark - 15); // '*', ';' missing
        }
        
        cout << car1.getMake() << endl;
        cout << car1.getModel() << endl;
        cout << car1.getColor() << endl;
        cout << car1.getLicenceNumber() << endl;
        cout << fineAmount << endl;
        cout << officerName << endl;
        cout << badgeNumber << endl; // 'g' missing
}
 
---------------------- driver.cpp------------------------
 
#include <iostream>
#include "ParkedCar.h"
#include "ParkingMeter.h"
#include "ParkingTicket.h"
 
using namespace std;
 
int main()
{
        ParkedCar car1;
        ParkingMeter meter1;
        meter1.setTime(100);
        car1.setMake("Ford");
        car1.setModel("Taurus");
        car1.setColor("Maroon");
        car1.setLicence(1337);
        car1.setMinutes(25);
        ParkingTicket ticket1(car1,25,"Lou",92);
        ticket1.setMinutesIllegalPark(15);
        cout << ticket1.getMinutesIllegalPark() << endl;
}

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
pacman32689Author Commented:
Sorry, I had fixed those earlier and forgot to update. I figured it out though, thanks for the help!
0
 
pacman32689Author Commented:
Thanks
0
 
Infinity08Commented:
>> I figured it out though, thanks for the help!

Note that the problem that directly caused this error :

        4 errors, telling me that ParkedCar is not a type inside my ParkingTicket.h file.

was the missing include (as jkr pointed out).
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now