I'm writing a project for univeristy. I wrote a function Vehicle.getActualCost(), tested it in VehicleTest.cpp and everything is working smoothly.
Problem occurs when in these tests I change Vehicle* to VehiclePtr as per professor's instruction.
VehiclePtr is a type declared in typedefs.h
file, which looks like this:
#ifndef CARRENTAL_TYPEDEFS_H
#define CARRENTAL_TYPEDEFS_H
#include "model/Client.h"
#include "model/Address.h"
#include "model/Rent.h"
#include "model/Vehicle.h"
typedef Address* AddressPtr;
class Rent;
typedef Rent* RentPtr;
class Client;
typedef Client* ClientPtr;
typedef Vehicle* VehiclePtr;
#endif //CARRENTAL_TYPEDEFS_H
After which I get errors like these:
In file included from /mnt/c/Users/szindzeks/CLionProjects/atom_cz_1215_03/majkowski/workshop/library/include/model/Client.h:8,
from /mnt/c/Users/szindzeks/CLionProjects/atom_cz_1215_03/majkowski/workshop/library/include/typedefs.h:8,
from /mnt/c/Users/szindzeks/CLionProjects/atom_cz_1215_03/majkowski/workshop/library/test/VehicleTest.cpp:6:
/mnt/c/Users/szindzeks/CLionProjects/atom_cz_1215_03/majkowski/workshop/library/include/model/Rent.h:24:5: error: ‘ClientPtr’ does not name a type
24 | ClientPtr client;
| ^~~~~~~~~
/mnt/c/Users/szindzeks/CLionProjects/atom_cz_1215_03/majkowski/workshop/library/include/model/Rent.h:26:5: error: ‘VehiclePtr’ does not name a type; did you mean ‘Vehicle’?
26 | VehiclePtr vehicle;
| ^~~~~~~~~~
| Vehicle
/mnt/c/Users/szindzeks/CLionProjects/atom_cz_1215_03/majkowski/workshop/library/include/model/Rent.h:40:33: error: ‘ClientPtr’ has not been declared
40 | Rent(const unsigned int id, ClientPtr client, VehiclePtr vehicle, pt::ptime beginTime);
| ^~~~~~~~~
/mnt/c/Users/szindzeks/CLionProjects/atom_cz_1215_03/majkowski/workshop/library/include/model/Rent.h:40:51: error: ‘VehiclePtr’ has not been declared
40 | Rent(const unsigned int id, ClientPtr client, VehiclePtr vehicle, pt::ptime beginTime);
| ^~~~~~~~~~
...
Client.h
#ifndef CARRENTAL_CLIENT_H
#define CARRENTAL_CLIENT_H
#include "Rent.h"
#include "typedefs.h"
#include <vector>
class Client {
private:
std::string firstName;
std::string lastName;
const std::string personalID;
AddressPtr address;
std::vector<Rent*> currentRents;
public:
Client(const std::string &firstName, const std::string &lastName, const std::string &personalID, AddressPtr address);
~Client();
std::string getInfo() const;
std::string getFullInfo() const;
const std::string &getFirstName() const;
const std::string &getLastName() const;
const std::string &getPersonalID() const;
const AddressPtr getAddress() const;
const std::vector<RentPtr> & getCurrentRents() const;
void setFirstName(const std::string &firstName);
void setLastName(const std::string &lastName);
void setAddress(AddressPtr address);
void addNewRent(RentPtr newRent);
void removeRent(unsigned int rentID);
void removeRent(RentPtr rentToRemove);
};
#endif //CARRENTAL_CLIENT_H
Rent.h
#ifndef CARRENTAL_RENT_H
#define CARRENTAL_RENT_H
#include "Vehicle.h"
#include "Client.h"
#include "typedefs.h"
#include <boost/date_time.hpp>
#include <math.h>
namespace pt = boost::posix_time;
namespace gr = boost::gregorian;
class Rent {
private:
const unsigned int id;
ClientPtr client;
VehiclePtr vehicle;
unsigned int rentCost = 0;
pt::ptime beginTime;
pt::ptime endTime = pt::not_a_date_time;
public:
Rent(const unsigned int id, ClientPtr client, VehiclePtr vehicle, pt::ptime beginTime);
const unsigned int getId() const;
const ClientPtr getClient() const;
const VehiclePtr getVehicle() const;
const pt::ptime &getBeginTime() const;
const pt::ptime &getEndTime() const;
std::string getInfo() const;
unsigned int getRentDays() const;
unsigned int getRentCost() const;
void endRent(pt::ptime endTime);
};
#endif //CARRENTAL_RENT_H
And the VehicleTest.cpp
#include <boost/test/unit_test.hpp>
#include "model/Bicycle.h"
#include "model/MotorVehicle.h"
#include "model/Car.h"
#include "model/Moped.h"
#include "typedefs.h"
BOOST_AUTO_TEST_SUITE(TestSuiteVehicle)
...
BOOST_AUTO_TEST_CASE(VehicleActualPriceTests){
VehiclePtr vehicle = new Vehicle("E", 1000);
...
VehiclePtr car5 = new Car("E", 1000, 2500, E);
BOOST_TEST(vehicle->getActualRentalPrice() == vehicle->getBasePrice());
BOOST_TEST(bicycle->getActualRentalPrice() == bicycle->getBasePrice());
...
BOOST_TEST(car5->getActualRentalPrice() == (int)(car5->getBasePrice() * 1.5 * 1.5));
delete vehicle;
...
delete car5;
}
I of course expected for it to work no problem after #inlucde "typedefs.h"
, but it didn't.
I tried different #include
variations, changing the typedefs.h
file but the effect was even worse or the same.
The program compiles and work as expected. The tests however, do not compile.
I'd be really thankful if someone was able to help me.
The file "typedefs.h"
includes headers "Rent.h"
and "Vehicle.h"
before declaring the typedefs.
#ifndef CARRENTAL_TYPEDEFS_H
#define CARRENTAL_TYPEDEFS_H
#include "model/Client.h"
#include "model/Address.h"
#include "model/Rent.h"
#include "model/Vehicle.h"
//.....
So the compiler issues errors because these headers use these typedefs before declaring last. At least the headers should be included after the typedefs. In the typedefs you can use elaborated class names.