“TypePtr 未在之后命名类型”,包括“typedefs.h”

问题描述 投票:0回答:1

我正在为大学写一个项目。我写了一个函数Vehicle.getActualCost(),在VehicleTest.cpp中测试了它,一切都很顺利。

在这些测试中,当我按照教授的指示将 Vehicle* 更改为 VehiclePtr 时,就会出现问题。 VehiclePtr 是在

typedefs.h
文件中声明的类型,如下所示:

#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

之后我收到如下错误:

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);
      |                                                   ^~~~~~~~~~

...

客户端.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

租金.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

还有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;
    }

我当然希望它在

#inlucde "typedefs.h"
之后能够正常工作,但事实并非如此。

我尝试了不同的

#include
变体,更改了
typedefs.h
文件,但效果更差或相同。

程序按预期编译并运行。但是测试无法编译。

如果有人能够帮助我,我将非常感激。

c++ cmake
1个回答
0
投票

文件

"typedefs.h"
在声明 typedef 之前包含标头
"Rent.h"
"Vehicle.h"

#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 之前。在 typedef 中,您可以使用详细的类名。

© www.soinside.com 2019 - 2024. All rights reserved.