未找到二进制 * 运算符
我有一个带有正确重载的 Vect*float 运算符的向量类,并尝试创建全局/非成员 float*Vect 运算符,如下所示:(注意这是一个经过大量编辑的示例)
class Vect
{
public:
Vect::Vect(const float p_x, const float p_y, const float p_z, const float p_w);
Vect operator*(const float p_sclr) const;
private:
float x;
float y;
float z;
float w;
};
Vect::Vect(const float p_x, const float p_y, const float p_z, const float p_w) {
x = p_x;
y = p_y;
z = p_z;
w = p_w;
}
Vect Vect::operator*(const float p_sclr) const {
return Vect( (x * p_sclr), (y * p_sclr), (z * p_sclr), 1); // reset w to 1
}
//Problem Non-MemberOperator
Vect operator*(const float p_sclr, const Vect& p_vect);
Vect operator*(const float p_sclr, const Vect& p_vect) {
return p_vect * p_sclr;
}
但是当我用调用:
Vect A(2.0f, 3.0f, 4.0f, 5.0f);
float s = 5.0f;
Vect C, D;
C = A * s; // Fine
D = s * A; // Error as below
我收到以下编译错误:
错误C2678:二进制'*':找不到运算符,它采用'float'类型的左操作数(或者没有可接受的转换)
任何人都可以提供关于为什么会发生这种情况的见解吗? MS 文档位于 http://msdn。 microsoft.com/en-us/library/ys0bw32s(v=VS.90).aspx 并且对 Visual Studio 2008 没有太大帮助。这是我收到的唯一编译错误或警告。
I have a vector class with a properly overloaded Vect*float operator and am trying to create the global/non-member float*Vect operator as follows: (Note this is a heavily edited sample)
class Vect
{
public:
Vect::Vect(const float p_x, const float p_y, const float p_z, const float p_w);
Vect operator*(const float p_sclr) const;
private:
float x;
float y;
float z;
float w;
};
Vect::Vect(const float p_x, const float p_y, const float p_z, const float p_w) {
x = p_x;
y = p_y;
z = p_z;
w = p_w;
}
Vect Vect::operator*(const float p_sclr) const {
return Vect( (x * p_sclr), (y * p_sclr), (z * p_sclr), 1); // reset w to 1
}
//Problem Non-MemberOperator
Vect operator*(const float p_sclr, const Vect& p_vect);
Vect operator*(const float p_sclr, const Vect& p_vect) {
return p_vect * p_sclr;
}
But when I go to test the operator with the call:
Vect A(2.0f, 3.0f, 4.0f, 5.0f);
float s = 5.0f;
Vect C, D;
C = A * s; // Fine
D = s * A; // Error as below
I receive the following compile error:
error C2678: binary '*' : no operator found which takes a left-hand operand of type 'float' (or there is no acceptable conversion)
Can anyone provide insight to why this happens? The MS documentation is available at http://msdn.microsoft.com/en-us/library/ys0bw32s(v=VS.90).aspx and isn't very helpful Visual Studio 2008. This is the only compile error or warning I receive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还没有发布完整示例。我可以毫无问题地编译以下代码:
所以,问题一定出在其他地方。
You still havn't posted a complete example. I can compile the following code without any problems:
So, the problem must lie somewhere else.
我还可以毫无问题地编译代码(就像 sellibitze 一样,他比我先一步!)
这是我使用的代码:
编辑:就像 sellibitze 所暗示的那样,问题可能出在其他地方。您列出的错误是编译器给您的唯一错误吗?另外,您正在运行什么版本的 Visual Studio?
I also can compile the code without any problems (like sellibitze, who beat me to it!)
Here's the code I used:
Edit: Like sellibitze suggests, the problem may lie elsewhere. Is the error you're listing the ONLY error your compiler is giving you? Also, what version of Visual Studio are you running?
看起来 2 打败了我,但它也对我有用 - 构建并运行良好(VS2010,Win32 控制台项目):
Looks like 2 beat me to it, but it worked for me too - built and ran fine (VS2010, Win32 console project):
最终的解决方案是使用友元函数,因为 p_vect 中的变量是私有的:
The final solution was to go with a friend function because the variables were in p_vect were private: