I am learning c++ and I am having a little trouble with this program. I converted one big program I created into classes and I am having trouble. I am new to classes. Can someone help. It will be greatly appreciated.
main.cpp
vehicle.cppCode:const int NUM_REPAIR_BAYS = 4;
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <queue>
#include "vehicle.h"
using namespace std;
// return available vehicle
Vehicle* vehicle_available(queue<Vehicle*>& q, queue<Vehicle*>& pq);
// return repair bay availabe
int repair_bay_available(Vehicle* repairBay[]);
int main()
{
int i;
queue<Vehicle*> q; // vehicle que
queue<Vehicle*> pq; // priority queue
Vehicle* repair_bay[NUM_REPAIR_BAYS]={0}; // repair bay
vector<Vehicle*> salvage_yard; // salvage yard
vector<Vehicle*> active_duty; // active duty
Vehicle* v;
int id = 0;
srand((unsigned int)time(0));
for(int t=0;t<5;t++)
{
// Create a number (0-4) new random vehicles per day
int n = rand() % 4;
// As the new vehicles are created they are placed into a queue
for(i=0;i<n;i++)
{
v = new Vehicle (id++);
cout << "creating vehicle: " << endl;
v->print();
q.push(v);
cout << endl;
}
// The members of the queue (#max/time) are then processed by a "yard manager
// The front of the queue is popped and added to: Salvage Yard
// or Repair Bay if available Vector[4]
v = vehicle_available(q,pq); // get available vehicle
if(v != NULL)
{
cout << "processing vehicle: " << endl;
v->print();
cout << endl;
i = repair_bay_available(repair_bay);
if(i >= 0)
{
v->print();
cout <<"was sent to repair bay #" << (i+1) << endl;
cout << endl;
repair_bay[i] = v;
}
else
{
cout << "All repair bays filled, vehicle sent to salvage yard" << endl;
salvage_yard.push_back(v);
}
// When a vehicle in a bay is finished it is then run to testing, each component is tested,
// if all pass the vehicle is returned to duty else returned to a priority queue which will pre-empt
// the repair queue (fastest first)
// for each repair bay
for(i=0;i<NUM_REPAIR_BAYS;i++)
{
v = repair_bay[i];
if(v != NULL)
{
if(v->passed_test()) // passed test?
{
v->print();
cout << "passed repair test, vehicle sent to active duty" << endl;
cout << endl;
active_duty.push_back(v);
repair_bay[i]=0;
}
else if(v->failed_test()) // failed test
{
v->print();
cout << "failed repair test, vehicle sent to priority queue" << endl;
cout << endl;
pq.push(v);
repair_bay[i]=0;
}
}
}
}
}
cout << endl;
// print out whats in queue
cout << "Vehicles in left in Queue: " << endl;
while(q.size() > 0)
{
print(q.front());
q.pop();
}
cout << endl;
// print out whats in priority queue
cout << "Vehicles left in priority Queue: " << endl;
while(pq.size() > 0)
{
print(pq.front());
pq.pop();
}
cout << endl;
// print out whats in storage bay
cout << "Vehicles in Repair Bay: " << endl;
for(i=0;i<NUM_REPAIR_BAYS;i++)
if(repair_bay[i] != 0)
print(repair_bay[i]);
cout << endl;
// print out whats in salvage yard
cout << "Vehicles in Salvage Yard: " << endl;
for(i=0;i<salvage_yard.size();i++)
print(salvage_yard[i]);
cout << endl;
// print out whats in active duty
cout << "Vehicles in Active Duty: " << endl;
for(i=0;i<active_duty.size();i++)
print(active_duty[i]);
cout << endl;
return 0;
}
//******************************************************************************
// return vehicle avaiable
Vehicle* vehicle_available(queue<Vehicle*>& q, queue<Vehicle*>& pq)
{
Vehicle* v=NULL;
// check priority queue first for vehicles
if(pq.size() > 0)
{
v = pq.front();
pq.pop();
}
// check queue for vehicles
else if(q.size() > 0)
{
v = q.front();
q.pop();
}
return v;
}
//******************************************************************************
// return repair bay availabLe
int repair_bay_available(Vehicle* repair_bay[])
{
for(int i=0;i<NUM_REPAIR_BAYS;i++)
{
if(repair_bay[i] == NULL)
return i;
}
return -1;
}
vehicle.hCode:#include <iostream>
#include <cstdlib>
#include <string>
#include "vehicle.h"
using namespace std;
void Vehicle::print()
{ cout << id << " " << Type << " ";
if(Armour[0]) cout << "F";
if(Armour[1]) cout << "S";
if(Armour[2]) cout << "R";
if(Armour[3]) cout << "P";
cout << " ";
cout << Weapons << " ";
if(Transmission) cout << "Transmission";
if(Differential) cout << "Differentisl";
cout << Engine << endl;
}
Vehicle::Vehicle (int _id)
{ id = _id;
int x = rand() % 4;
// V1, V2, etc)
Type = "V1";
if(x == 1)
Type = "V2";
if(x == 2)
Type = "V3";
if(x == 3)
Type = "V4";
Armour[0] = true; // bool: F,S,R,P)
Armour[1] = rand() % 2==0;
Armour[2] = rand() % 2==0;
Armour[3] = rand() % 2==0;
x = rand() % 3 + 1;
if(x == 1)
Weapons = "w1"; // (list of strings w1, w2, w3...etc)
if(x == 2)
Weapons = "w1, w2";
if(x == 3)
Weapons = "w1, w2, w3";
if(x == 4)
Weapons = "w1, w2, w3, w4";
// Drive train
Transmission = rand() * 2 == 0;
Differential = rand() * 2 == 0;
x = rand() % 3;
if(x == 0)
Engine = 'O'; // (O,N,D) O-operational N-No engine D-Damaged
if(x == 1)
Engine = 'N';
if(x == 2)
Engine = 'D';
}
// return true if vehicle pased test
bool Vehicle::passed_test()
{
// 50 % vehicle pass
if(rand()%100>50)
{
// set repaired
Transmission=false;
Differential=false;
Engine='O';
return true;
}
else return false;
}
// return false if vehicle pass test
bool Vehicle::failed_test()
{
return rand()%100<20;
}
errorCode:using namespace std;
// Vehicle info
class Vehicle
{
int id; // -Id number
string Type; // V1, V2, etc)
bool Armour[4]; // bool: F,S,R,P)
string Weapons; // (list of strings w1, w2, w3...etc)
// Drive train
bool Transmission;
bool Differential;
char Engine; // (O,N,D) O-operational N-No engine D-Damaged
public:
Vehicle (int id);
void print ();
bool passed_test();
bool failed_test();
};
Code:In function 'int main()':
error: 105: 'print' was not declared in this scope
error: 114: 'print' was not declared in this scope
error: 124: 'print' was not declared in this scope
warning: 129: comparison between signed and unsigned integer expressions
error: 130: 'print' was not declared in this scope
warning: 135: comparison between signed and unsigned integer expressions
error: 136: 'print' was not declared in this scope