如何将对象作为参数传递
How to pass Object as parameter
我是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 中。将其放在公共场所
尝试
相关文章:
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 如何将对象数组作为参数传递给模板
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- 对象作为参数传递,就好像我们正在传递构造函数值一样
- 初始化对象以在 C++08 中作为参数传递的首选语法是什么?
- 将对象作为参数传递,没有可行的重载"="
- 当我按值传递参数时对象被破坏时?
- 在将匿名对象作为参数传递时,不会调用任何构造函数
- 是否可以创建一个指向对象的智能指针并将此智能指针作为参数传递给构造函数?
- C++:将对象作为参数传递
- 将 std::ofstream 对象作为参数传递给类方法
- 将对象向量的指针作为参数传递,但差异更大
- 当数组对象以函数参数传递时,为什么复制构造函数会自称
- 如何将对象作为参数传递
- 将对象作为C 中的参数传递
- 将新对象的名称作为参数传递
- 将对象数组作为参数传递的最佳方法是什么
- 为什么将临时对象作为参数传递需要 std::move?
- 将任何数据类型/对象作为参数传递以确定其大小