typedef in class. In another class error: type has not been declared

1.6k Views Asked by At

In Hire.h i declared two typedefs. In Customer.h i would be able to use this typedefs. How to fix it?

#pragma once
#include "typedefs.h"
#include "Vehicle.h"
#include "Customer.h"
namespace rent {

typedef std::shared_ptr<Hire> shared_Hire_t;
typedef std::map<uuid_t, shared_Hire_t> mapHirePtr_t;

    class Hire
    {

    public:
    Hire(date_t start_date, Vehicle *vehicle, Customer *customer);
    virtual ~Hire();                                    

    private:
    uuid_t uuid;
    date_t start_date;
    date_t end_date;
    uint_t hire_days;
    double day_price;
    double cost;
    Vehicle *vehicle;   
    Customer *customer;

    };
    }

-

#pragma once
#include <iostream>
#include <map>
#include <memory>
#include "typedefs.h"   //uint_t, uuid_t
#include "CustomerType.h"

namespace rent {

class Hire;

class Customer
{
public:
enum EnumCustomerType {standard, medium, premium};  //to set a CustomerType
Customer(std::string name, std::string surname, std::string pesel, double balance = 0.0, EnumCustomerType enum_customer_type = standard);
virtual ~Customer();

void add_hire(shared_Hire_t hire);
void end_hire(shared_Hire_t hire);

protected:

std::string name;
std::string surname;
std::string pesel;
double balance;
EnumCustomerType enum_customer_type;
double discount;

mapHirePtr_t current_hires;
uint_t hires_count;

private:

std::unique_ptr<CustomerType> customer_type;

};
}

-

Customer.h:31:17: error: 'shared_Hire_t' has not been declared void add_hire(shared_Hire_t hire); ^ Customer.h:32:31: error: 'shared_Hire_t' has not been declared void end_hire(shared_Hire_t hire); ^ Customer.h:42:3: error: 'mapHirePtr_t' does not name a type mapHirePtr_t current_hires;
^ Customer.cpp:87:14: error: prototype for 'void rent::Customer::add_hire(rent::shared_Hire_t)' does not match any in class 'rent::Customer' void Customer::add_hire(shared_Hire_t hire) ^ In file included from Customer.cpp:1:0: Customer.h:31:8: error: candidate is: void rent::Customer::add_hire(int) void add_hire(shared_Hire_t hire); ^ Customer.cpp:94:14: error: prototype for 'void rent::Customer::end_hire(rent::shared_Hire_t)' does not match any in class 'rent::Customer' void Customer::end_hire(shared_Hire_t hire) ^ Customer.cpp:1:0: Customer.h:32:22: error: candidate is: void rent::Customer::end_hire(int) void end_hire(shared_Hire_t hire);

PS. How to separate two blocks of code in an appropriate way in stackoverflow?

2

There are 2 best solutions below

2
On

You have to include Hire.h in Customer.h for the typedefs to be visible. As that would introduce a circular reference i would suggest putting the typedefs in a seperate file.

0
On

You need to #include "Hire.h in Customer.h to make the definitions available there.

Unfortunately, that creates a circular dependency between the headers.

Fortunately, Hire.h doesn't need the definitions of Customer and Vehicle, so you can stop including them and just forward-declare the classes.

Hire.h:

#include "typedefs.h"
namespace rent {

class Vehicle;
class Customer;
class Hire;

typedef std::shared_ptr<Hire> shared_Hire_t;
typedef std::map<uuid_t, shared_Hire_t> mapHirePtr_t;

class Hire
{


// ...

Customer.h:

#include <iostream>
#include <map>
#include <memory>
#include "typedefs.h"   //uint_t, uuid_t
#include "CustomerType.h"
#include "Hire.h"

namespace rent {

class Customer
{
// ...