【发布时间】:2019-03-12 14:56:19
【问题描述】:
我一直在编写一些代码来尝试为某种出租车服务建模,但我遇到了一些问题。
我有一个类RunServer,它查看用户给出的命令的状态(src::control::Global::stat_commandPath)并要求额外的输入基于这些命令,然后再使用该输入进行操作。
问题是我得到一个“错误:预期的类型说明符”(GCC-7.3.0,C++11),它看起来可能与我如何命名类有关.如果从 src/Vehicle/Car.h 中删除 namespace 声明,则此问题不再发生。
这应该是这个问题的所有相关代码。抱歉,内容太多了,我已经截断了所有看起来没有影响的内容。问题在于 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
【问题讨论】:
-
在应用程序域中嵌套命名空间通常是个坏主意,尤其是对于初学者。
namespace src到底是什么意思? -
namespace src是指该文件位于 src/... 目录下。我习惯了 Haxe,所以我试图模仿它的 module and path syntax -
这对于 C++ 代码来说似乎不是一件好事——类型/类/命名空间名称与 C++ 中的目录或文件名之间没有关系。
-
好的,那我试试下划线分隔的命名空间。
-
不,这也不是一个好主意 - 只是摆脱命名空间。你真的读过 C++ 教科书吗?
标签: c++ class namespaces