如何将对象作为参数传递

How to pass Object as parameter

本文关键字:参数传递 对象      更新时间:2023-10-16

我是C 的新手,所以我可能会有很多错误。我来自C#,并且对C 中的方法并不熟悉。

所以我有一个电梯课,一个叉车课和一个板条箱类。我需要将电梯传递到叉车的构造函数。据我所研究的方法是这样做的方法,但我仍然会遇到错误。

在班级板条箱中,我通过一个对象并起作用。

Forklift::Forklift(list<Crate> crates, Elevator & elevator)
{
    for each (Crate crate in crates)
    {
        this->cratestack.push(crate);
    }
    this->elevator = elevator;
}

它说电梯没有默认的构造函数。我知道,如果我尚未定义一个编译器,则不会像C#一样生成一个编译器,但是当我不使用默认值时,我是否需要它?我需要具有这些参数的电梯。

这是叉车的头文件:

class Forklift
{
private:
    Elevator elevator;
    stack<Crate> cratestack;
    stack<Crate> helperstack;
public:
    Forklift(list<Crate> crates,Elevator& elevator);
    ~Forklift();
    int start();
};

这是电梯头:

class Elevator
{
private:
    int minweight;
    int maxweight;
    int currentweight;
    int tours;
public:
    Elevator(int max, int min);
    ~Elevator();
    bool Load(Crate crate);
    int GetTour();
};

和源文件:

Elevator::Elevator( int max, int min)
{
    this->maxweight = max;
    this->minweight = min;
}
Elevator::~Elevator()
{
}
bool Elevator::Load(Crate crate)
{
    if (currentweight + crate.GetWeight() > maxweight)
    {
        return false;
    }
    else
    {
        return true;
    }
}
int Elevator::GetTour() 
{
    return this->tours;
}

构造函数也位于scop private 中。将其放在公共场所

尝试