C++包括类名间距和类实例化
C++ includes, class namespacing, and class instantiation
我一直在编写一些代码来尝试对某种出租车服务进行建模,但我遇到了一些问题。
我有一个类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
解决了此问题。
相关文章:
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- ATL::CComContainedObject<contained>: C2259 无法实例化抽象类
- 尝试实例化模板类的对象时出现"No Matching Constructor"错误
- 如何使用其中一个具体类实例化抽象类?
- 在实例化封闭类模板之后,我们可以声明模板类成员的部分专用化吗
- 在编译时检查未实例化的类模板是否继承自其第一个模板参数
- 无法实例化抽象类错误
- 实例化新类时未调用的方法
- 无法实例化抽象类,但类不是抽象/派生方法的参数
- 访问使用接口实例化的类的私有成员
- 如何在不实例化派生类的情况下管理一组派生类枚举
- 'AMA::我的产品':无法实例化抽象类
- 如何编写模板重载函数,并在模板参数不允许实例化某个类时触发回退
- 无法实例化抽象类
- 如何修复错误C2259:无法实例化抽象类
- C++,不能实例化抽象类
- C++ 实例化派生类的语法异常
- 如何创建无法实例化的类
- 无法实例化简单类