C ++包括类命名空间和类实例化

时间:2018-10-07 23:24:41

标签: c++ class namespaces

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

我有一个 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

2 个答案:

答案 0 :(得分:0)

这是完全错误的:

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

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

[AVAudioPCMBuffer]

答案 1 :(得分:0)

问题似乎与案件有关。似乎名称空间声明不区分大小写。将<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.0</TargetFramework> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <PackageConflictPreferredPackages>Microsoft.Private.CoreFx.NETCoreApp.4.6.0-preview1-27013-1;runtime.win-x64.Microsoft.Private.CoreFx.NETCoreApp.4.6.0-preview1-27013-1;$(PackageConflictPreferredPackages)</PackageConflictPreferredPackages> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.Private.CoreFx.NETCoreApp" Version="4.6.0-preview1-27013-1" /> </ItemGroup> 的命名空间更改为src::vehicle::Car可以解决此问题。