c++ - 无法访问引用 C++ 背后的信息

标签 c++ eclipse function reference return

尝试对两个 vector 进行减法运算。最后它应该像这样工作:

vector1.sub(vector2);

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

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/

相关文章:

c++ - 指针的问题

eclipse - libgdx 无法在 ubuntu 上启动桌面项目

c - Eclipse + OpenCV + Cygwin --> cv.h : No such file or directory

java - IntelliJ IDEA 与 Eclipse 的 Maven 嵌套多模块项目中的 Jetty 资源库不同

c++ - 在嵌套的 if 语句中使用函数来触发 Action

r - 将矩阵应用于函数

c++ - 在 C++ 中什么时候需要指向派生类对象的基类指针?

c++ - 如何返回嵌套在其他包中的模板包?

c++ - 使用 IFilter 检索的文本的编码是什么?

c++ - 结构的结构作为参数