【发布时间】:2010-05-17 04:19:15
【问题描述】:
我有以下功能(在 Visual Studio 中工作):
bool Plane::contains(Vector& point){
return normalVector.dotProduct(point - position) < -doubleResolution;
}
当我使用 g++ version 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&。
解决此问题的最佳方法是什么?
我已验证这可行:
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) {
}
【问题讨论】:
-
关于“在 Visual Studio 中工作”问题,Visual C++ 有一个语言扩展,允许将临时引用绑定到非常量引用。您可以在项目属性中禁用语言扩展,如果您想编写可移植代码,这样做可能是个好主意。
标签: c++ visual-studio g++ pass-by-reference