【问题标题】:cant access information behind reference c++无法访问参考 C++ 后面的信息
【发布时间】:2018-05-11 11:36:37
【问题描述】:

试图做两个向量的减法。最后它应该像这样工作:

vector1.sub(vector2);

自定义变量向量被定义为:Vektor(double x, double y, double z)。 现在我想通过input.x等访问xyz坐标。 告诉我

conversion from 'Vektor*' to non scalar type 'Vektor' requested.

为什么难???难道不能从一个值中减去对一个值的引用吗?

顺便说一句,我是新来的,所以无论我做错了什么,请随意烤我!;)

Vektor Vektor::sub(const Vektor& input) const
{
    Vektor subresult = new Vektor(x - input.x, y - input.y, z - input.z);
    return subresult;
}

【问题讨论】:

  • new 返回一个指针,只是不需要。 C++ 不是 Java。 :-)

标签: c++ eclipse function reference return


【解决方案1】:

这里不要使用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 类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多