C++对象与对象引用的类型错误

发布于 2024-09-01 23:19:39 字数 1018 浏览 7 评论 0原文

我有以下函数(在 Visual Studio 中工作):

bool Plane::contains(Vector& point){
    return normalVector.dotProduct(point - position) < -doubleResolution;
}

当我使用 g++ 版本 4.1.2 编译它时,出现以下错误:

Plane.cpp: In member function âvirtual bool Plane::contains(Vector&)â:
Plane.cpp:36: error: no matching function for call to âVector::dotProduct(Vector)â
Vector.h:19: note: candidates are: double Vector::dotProduct(Vector&)

所以如您所见,编译器认为 (point-position) 是一个 Vector 但它期望矢量&.

解决这个问题的最佳方法是什么?

我证实这有效:

Vector temp = point-position;
return normalVector.dotProduct(temp) < -doubleResolution;

但我希望有一些更干净的东西。

我听到一个建议,添加复制构造函数可能会有所帮助。所以我向 Vector 添加了一个复制构造函数(见下文),但它没有帮助。

矢量.h:

Vector(const Vector& other);

矢量.cpp:

Vector::Vector(const Vector& other)
    :x(other.x), y(other.y), z(other.z), homogenous(other.homogenous) {
}

I have the following function (which worked in Visual Studio):

bool Plane::contains(Vector& point){
    return normalVector.dotProduct(point - position) < -doubleResolution;
}

When I compile it using g++ version 4.1.2 , I get the following error:

Plane.cpp: In member function âvirtual bool Plane::contains(Vector&)â:
Plane.cpp:36: error: no matching function for call to âVector::dotProduct(Vector)â
Vector.h:19: note: candidates are: double Vector::dotProduct(Vector&)

So as you can see, the compiler thinks (point-position) is a Vector but it's expecting Vector&.

What's the best way to fix this?

I verified that this works:

Vector temp = point-position;
return normalVector.dotProduct(temp) < -doubleResolution;

But I was hoping for something a little bit cleaner.

I heard a suggestion that adding a copy constructor might help. So I added a copy constructor to Vector (see below), but it didn't help.

Vector.h:

Vector(const Vector& other);

Vector.cpp:

Vector::Vector(const Vector& other)
    :x(other.x), y(other.y), z(other.z), homogenous(other.homogenous) {
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笛声青案梦长安 2024-09-08 23:19:42

问题是您的 dotProduct 函数应该通过 const 引用获取其参数。

The problem is that your dotProduct function should take its parameter by const reference.

深居我梦 2024-09-08 23:19:41

您的问题是 point -position 的结果是一个临时对象,无法绑定到非常量引用。

如果函数不修改通过引用获取的参数,那么它应该采用 const 引用。因此,您的点积函数应声明为:

double Vector::dotProduct(const Vector&);

Your problem is that the result of point - position is a temporary object, which cannot be bound to a non-const reference.

If a function does not modify an argument taken by reference, then it should take a const reference. Ergo, your dot product function should be declared as:

double Vector::dotProduct(const Vector&);
書生途 2024-09-08 23:19:41

Vector 临时变量无法正确转换为 Vector& ——我猜 MSVC++ 在这里太宽松了。为什么 containsdotProduct 采用 Vector& 而它们实际上不需要修改参数?!他们应该采用 const Vector&!我认为海湾合作委员会在这里正确地指导你。

A Vector temporary variable can't properly be converted to a Vector& -- I guess MSVC++ is being too lax here. Why do contains and dotProduct take a Vector& where they never really need to modify the arg?! They should take a const Vector&! I think gcc is guiding you correctly here.

无风消散 2024-09-08 23:19:41

point -position 似乎创建了 Vector 类型的临时对象,并且您试图将临时对象传递给需要引用的函数。这是不允许的。尝试将其声明为 dotProduct(const Vector&);

point - position seems to creates temporary object of type Vector and you are trying to pass temporary to a function that requires reference. It is not allowed. Try declare it as dotProduct(const Vector&);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文