简体   繁体   English

将派生类中的对象插入到该对象的另一个实例中(使用vector.insert函数)C ++

[英]Inserting an object from a derived class into another instance of said object (using the vector.insert function) C++

I am currently working through code which has a Road class. 我目前正在通过具有Road类的代码进行工作。 This has a vector of lanes and includes a lane class. 它具有车道向量,并包括车道类。 The lane class has a vector of vehicles and each vehicle has its derived classes (Car, Motorcycle, Lorry). 车道类别具有车辆向量,每辆车都有其派生类别(汽车,摩托车,货车)。 I would like a vehicle to be able to assess whether it can move into another lane, ie be inserted into another lane's vector of vehicles, safely (a car requires some safe distance, so I have already implemented how a vehicle knows if it is clear to switch lanes). 我希望车辆能够安全地评估它是否可以移动到另一个车道,即插入到另一个车道的车辆矢量中(汽车需要一定的安全距离,因此我已经实现了如何知道车辆是否畅通无阻切换车道)。

void Road::Simulate(double time)
{

for (unsigned int iLane = 0; iLane < getNLanes()-1; iLane++)
{
    for (unsigned int iV = 0; iV < getCurrentLane(iLane)->getNVehiclesinLane(); iV++)
    {
        std::cout<< " AllowedOvertake TEST "<<   getCurrentLane(iLane+1)->allowedOvertake(getCurrentLane(iLane)->getCurrentVehicle(iV)->getPosition(), getCurrentLane(iLane)->getCurrentVehicle(iV)->getMinDistance())<<std::endl;
        getCurrentLane(iLane+1)->setCanOvertake(allowedOvertake(getCurrentLane(iLane)->getCurrentVehicle(iV)->getPosition(), getCurrentLane(iLane)->getCurrentVehicle(iV)->getMinDistance()));

        if (getCurrentLane(iLane+1)->getCanOvertake() == true)
        {
            getCurrentLane(iLane+1)->insertVehicle(getCurrentLane(iLane)->getCurrentVehicle(iV), 0);
            delete getCurrentLane(iLane)->getCurrentVehicle(iV);
        }

    }
}
for (unsigned int iLane = 0; iLane < getNLanes(); iLane++)
{
    getCurrentLane(iLane)->Simulate(time);

} } }}

I loop over all the present lanes, except the last one as any vehicle in this lane cannot overtake. 我绕过了所有当前车道,但最后一条车道除外,因为该车道中的任何车辆都无法超车。 After looping over the vehicles contained in each lane, I have a function which returns a Boolean which confirms whether an overtake scenario can be executed. 在遍历每个车道中包含的车辆之后,我有一个函数返回一个布尔值,该布尔值确认是否可以执行超车场景。 This is done in allowedOvertake() . 这是在allowedOvertake() If this returns true, I implement an insert function. 如果返回true,则实现一个插入函数。

My issue/question: How can I make this ideology work, and whether it is sensible to have these setCanOvertake() and getCanOvertake() functions. 我的问题是:如何使这种意识形态起作用,以及具有这些setCanOvertake()getCanOvertake()函数是否明智。

One possible solution could be to just pushback a new vehicle into the intended lane, but with the appropriate positions, velocities etc. However, I am not sure how to ensure that the vehicle being entered has the same type (Car, Lorry...) too. 一种可能的解决方案是将一辆新车推回原先的车道,但要有适当的位置,速度等。但是,我不确定如何确保所输入的车辆具有相同的类型(汽车,货车... )。

Currently, I do not get any build errors if I exclude the insertVehicle() function, and I have vehicle motion being drawn using QPainter. 当前,如果我排除了insertVehicle()函数,则不会出现任何构建错误,并且使用QPainter绘制了车辆运动。 However, with the insertVehicle() function, I do not get any build errors but I do get a crash once I run the project. 但是,使用insertVehicle()函数,我没有得到任何构建错误,但是一旦运行项目,我确实会崩溃。

Any help would be appreciated, and apologies for any coding errors (I'm a keen, but very inexperienced C++ user). 任何帮助将不胜感激,对于任何编码错误,我深表歉意(我是一位敏锐的,但经验不足的C ++用户)。

For reference, I have the above functions' definitions as follows 供参考,我对上述函数的定义如下

bool Lane::allowedOvertake(double pos, double mindist)
{
    for (unsigned int iV = 0; iV < getNVehiclesinLane() - 1; iV++)
    {
        if ((fVehicles[iV]->getPosition() > pos - mindist)// If inside rear safety distance.
            || fVehicles[iV]->getPosition() < pos + mindist)// If inside front safety distance.
       {}//continue
    else {return false;}
    }
return true;
}

//IN Lane.h
bool getCanOvertake() const{return FREE_LANE_OVERTAKE;}
//IN Lane.h
void setCanOvertake(bool overtake = true){FREE_LANE_OVERTAKE = overtake;}

Apologies, I was under the impression I had referenced my insertVehicle() function definition. 抱歉,给我的印象是我引用了insertVehicle()函数定义。

void Lane::insertVehicle(Vehicle*v, int ielement) {

    Vehicle* vins = new Vehicle(v->getPosition(), v->getVelocity(), v->getAcceleration());

    for (unsigned int iDist = 0; iDist < fVehicles.size()+1; iDist++){fVehicles.insert(fVehicles.begin() + (ielement+1), vins);}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++ std :: vector.insert在调试时崩溃,但在发行时有效 - c++ std::vector.insert crashes on debug but works on release 派生类中的C ++ Enforce函数接受派生类的对象 - C++ Enforce function in derived class that accepts an object of the derived class c ++将对象从一个类推到另一个类的向量中? - c++ Pushing an object from one class into a vector in another class? 如何确定一个object是否是某个派生的实例C++ class从指向GDB中的基数class的指针? - How to determine if an object is an instance of certain derived C++ class from a pointer to a base class in GDB? 将一个类中的对象用作另一个类的对象C ++ - Using an object from a class as an object for another class c++ 检查一个对象的类是否从 C++ 中的另一个对象的类派生 - Check if class of one object is derived from class of another object in C++ c++:从另一个派生类型创建派生 object - c++: create derived object from another derived type C ++向量生成函数类对象 - c++ vector generate function class object 从基本向量C ++中的对象调用派生方法 - Call a derived method from a object in a base vector c++ 从C ++中另一个(无关!)类的基类引用派生类的对象 - Referring to an object of a derived class from the base class of another (unrelated!) class in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM