在Hire.h中我声明了两个typedef。在Customer.h中,我将能够使用此typedef。如何解决?
#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:错误:&#39; shared_Hire_t&#39;尚未宣布无效 add_hire(shared_Hire_t hire); ^ Customer.h:32:31:错误:&#39; shared_Hire_t&#39;尚未宣布 void end_hire(shared_Hire_t hire); ^ Customer.h:42:3:错误:&#39; mapHirePtr_t&#39;没有命名类型mapHirePtr_t current_hires;
^ Customer.cpp:87:14:错误:原型为&#39; void 租金::客户:: add_hire(租金:: shared_Hire_t)&#39;与任何人都不匹配 class&#39; rent :: Customer&#39; void Customer :: add_hire(shared_Hire_t hire) ^在Customer.cpp中包含的文件中:1:0:Customer.h:31:8:错误:候选者是:void 租:::客户:: add_hire(int)void add_hire(shared_Hire_t hire); ^ Customer.cpp:94:14:错误:原型为&#39; void rent :: Customer :: end_hire(rent :: shared_Hire_t)&#39;与任何人都不匹配 class&#39; rent :: Customer&#39; void Customer :: end_hire(shared_Hire_t hire) ^ Customer.cpp:1:0:Customer.h:32:22:错误:候选者是:void rent :: Customer :: end_hire(int) void end_hire(shared_Hire_t hire);
PS。如何在stackoverflow中以适当的方式分离两个代码块?
答案 0 :(得分:0)
您必须在Customer.h中包含Hire.h才能使typedef可见。 因为这会引入一个循环引用,我建议将typedef放在一个单独的文件中。
答案 1 :(得分:0)
您需要在Customer.h中#include "Hire.h
在那里提供定义。
不幸的是,这会在标题之间产生循环依赖。
幸运的是,Hire.h不需要Customer
和Vehicle
的定义,因此您可以停止包含它们,只需向前声明类。
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
{
// ...