【问题标题】:C++ includes, class namespacing, and class instantiationC++ 包括类命名空间和类实例化
【发布时间】: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.hsrc/vehicle/Van。 hsrc/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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这是完全错误的:

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

    【讨论】:

    • 我以为你可以声明多个相同类型的函数/变量,用逗号分隔它们?
    • 我过去看到过变量,但即使这样也是一种不好的做法。然后你可能想给我们更多的细节,比如错误和它来自哪里(即行)。
    • Dangit 我以为我忘记了什么!我马上把它放进去。
    • @0x.dummyVar 不,你不能声明多个用逗号分隔的函数。
    • 是的,它可能是错误的形式,我会改变它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2010-11-21
    • 1970-01-01
    • 2019-12-23
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多