C++包括类名间距和类实例化

C++ includes, class namespacing, and class instantiation

本文关键字:实例化 包括类 C++      更新时间:2023-10-16

我一直在编写一些代码来尝试对某种出租车服务进行建模,但我遇到了一些问题。

我有一个类RunServer,它查看用户给出的命令的状态(src::control::Global::stat_commandPath(,并在使用该输入执行某些操作之前根据这些命令请求其他输入。

问题是我收到一个"错误:预期的类型说明符"(GCC-7.3.0,C++11(,看起来它可能与我如何命名类有关。如果从src/Vehicle/Car.h中删除命名空间声明,则此问题将停止发生。

这应该是此问题的所有相关代码。对不起,它太多了,我已经截断了所有看起来没有影响的东西。问题出在src/control/RunServer.h第 66、70 和 74 行。src/vehicle/Pickup.h和 src/vehicle/Van.h 与 src/vehicle/Car.h具有相同的结构。

src/control/Global.h

#ifndef INCLUDED_src_control_Global_h
#define INCLUDED_src_control_Global_h
#include <string>
#include "../vehicle/Vehicle.h"

namespace src {
namespace control {

class Global final
{
virtual void instantiable() = 0;
private:
static size_t
stat_vehicleArrayLength;
static src::Vehicle
** stat_vehicleArray;
public:
static std::string
stat_commandPath,
stat_stdcoutEnd;
public:
static bool
// Deletes the pointer argument if adding fails.
add_vehicle(
src::Vehicle *
),
exists_vehicle(
std::string
),
remove_vehicle(
std::string
);
static size_t
get_vehicleAmount(),
position_vehicle(
std::string
);
static src::Vehicle
** get_vehicles();
};

}}

#endif

src/control/RunServer.cpp

#include <iostream>
#include <stdlib.h>  // exit()
#include <string>
#include <regex>
#include "../../lib/StringTools.h"
#include "../vehicle/Car.h"
#include "../vehicle/Pickup.h"
#include "../vehicle/Van.h"
#include "../vehicle/VehicleType.h"
#include "../person/Driver.h"
#include "../person/Passenger.h"
#include "Global.h"
#include "RunServer.h"

inline bool
src::control::RunServer::navigation(
std::string input)
{
if (input == "return")
{
src::control::Global::stat_commandPath.pop_back();
return true;
}
if (input == "exit")
{
exit(0);
return true;
}
return false;
}
void
src::control::RunServer::run()
{
std::string input;
// "0"   ~ Create...
// "00"  ~ Create > Vehicle...
// "000" ~ Create > Vehicle > Car
// "001" ~ Create > Vehicle > Pickup
// "002" ~ Create > Vehicle > Van
// "01"  ~ Create > Person...
// "010" ~ Create > Person > Driver
// "011" ~ Create > Person > Passenger
// "1"   ~ Destroy...
// "10"  ~ Destroy > Vehicle
// "11"  ~ Destroy > Passenger
// "2"   ~ Print
if (src::control::Global::stat_commandPath == "000" || src::control::Global::stat_commandPath == "001" || src::control::Global::stat_commandPath == "002")
{
// Create > Vehicle > (Car|Pickup|Van).
std::cout << "n";
std::cout << "<vehicle identification (char array)>" << src::control::Global::stat_stdcoutEnd;
getline(std::cin, input);
if (src::control::RunServer::navigation(input))
{
return;
}
if (std::regex_match(input, std::regex("\w+")))
{
if (src::control::Global::stat_commandPath.back() == '0' && !src::control::Global::add_vehicle(new src::vehicle::Car(input)))
{
std::cout << "nA vehicle with this identifier already exists!n";
}
else if (src::control::Global::stat_commandPath.back() == '1' && !src::control::Global::add_vehicle(new src::vehicle::Pickup(input)))
{
std::cout << "nA vehicle with this identifier already exists!n";
}
else if (src::control::Global::stat_commandPath.back() == '2' && !src::control::Global::add_vehicle(new src::vehicle::Van(input)))
{
std::cout << "nA vehicle with this identifier already exists!n";
}
else
{
std::cout << "nAn error occured!n";
}
}
}
}

src/vehicle/car.h

#ifndef INCLUDED_src_vehicle_Car_h
#define INCLUDED_src_vehicle_Car_h
#include <string>
#include "Vehicle.h"

namespace src {
namespace vehicle {

class Car final : public src::Vehicle
{
void instantiable() override {};
public:
Car();
Car(
std::string
);
int
canAddPassenger(
src::person::Passenger *
) override;
};

}}

#endif

这是完全错误的:

static bool
// Deletes the pointer argument if adding fails.
add_vehicle(
src::Vehicle *
),
exists_vehicle(
std::string
),
remove_vehicle(
std::string
);

你想在这里完成什么?这不是应该是 3 个独立的原型吗?

static bool
// Deletes the pointer argument if adding fails.
add_vehicle(
src::Vehicle *
);
static bool exists_vehicle(
std::string
);
static bool remove_vehicle(
std::string
);

问题似乎与案例有关。似乎命名空间查找不区分大小写,而命名空间声明是。将src::vehicle::Car的命名空间更改为src::avehicle::Car解决了此问题。