C++对象与对象引用的类型错误
我有以下函数(在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是您的 dotProduct 函数应该通过 const 引用获取其参数。
The problem is that your dotProduct function should take its parameter by const reference.
您的问题是
point -position
的结果是一个临时对象,无法绑定到非常量引用。如果函数不修改通过引用获取的参数,那么它应该采用 const 引用。因此,您的点积函数应声明为:
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:
Vector
临时变量无法正确转换为Vector&
——我猜 MSVC++ 在这里太宽松了。为什么contains
和dotProduct
采用Vector&
而它们实际上不需要修改参数?!他们应该采用const Vector&
!我认为海湾合作委员会在这里正确地指导你。A
Vector
temporary variable can't properly be converted to aVector&
-- I guess MSVC++ is being too lax here. Why docontains
anddotProduct
take aVector&
where they never really need to modify the arg?! They should take aconst Vector&
! I think gcc is guiding you correctly here.point -position
似乎创建了Vector
类型的临时对象,并且您试图将临时对象传递给需要引用的函数。这是不允许的。尝试将其声明为dotProduct(const Vector&);
point - position
seems to creates temporary object of typeVector
and you are trying to pass temporary to a function that requires reference. It is not allowed. Try declare it asdotProduct(const Vector&);