尝试对两个 vector 进行减法运算。最后它应该像这样工作:
vector1.sub(vector2);
自定义变量 Vektor 定义为:Vektor(double x, double y, double z)
。
现在我想通过input.x
等访问x
、y
、z
坐标。
告诉我
conversion from 'Vektor*' to non scalar type 'Vektor' requested.
为什么难???不能从值中减去对值的引用吗?
顺便说一句,我是 SO 的新手,所以无论我做错了什么,都可以随意批评我!;)
Vektor Vektor::sub(const Vektor& input) const
{
Vektor subresult = new Vektor(x - input.x, y - input.y, z - input.z);
return subresult;
}
最佳答案
你不应该在这里使用new
,只是按值返回
Vektor Vektor::sub(const Vektor& input) const
{
return Vektor(x - input.x, y - input.y, z - input.z);
}
另请注意 you can override operator-
因此您可以使用语法 v1 - v2
执行减法,其中每个都是 Vektor
类型。
关于c++ - 无法访问引用 C++ 背后的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50291504/