未找到二进制 * 运算符

发布于 2024-09-25 01:07:05 字数 1279 浏览 3 评论 0原文

我有一个带有正确重载的 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 技术交流群。

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

发布评论

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

评论(4

眼泪都笑了 2024-10-02 01:07:05

您还没有发布完整示例。我可以毫无问题地编译以下代码:

class vect
{
    float coeffs[4];
public:
    vect()
    {
        for (int k=0; k<4; ++k)
            coeffs[k] = 0;
    }

    vect(float x, float y, float z, float w)
    {
        coeffs[0] = x;
        coeffs[1] = y;
        coeffs[2] = z;
        coeffs[3] = w;
    }

    vect operator*(float scalar) const
    {
        return vect(
            scalar*coeffs[0],
            scalar*coeffs[1],
            scalar*coeffs[2],
            scalar*coeffs[3] );
    }
};

vect operator*(float scalar, vect const& x)
{
    return x*scalar;
}

void test()
{
    vect a (2,3,4,5);
    float s = 5;
    vect c, d;
    c = a * s;
    d = s * a;
}

所以,问题一定出在其他地方。

You still havn't posted a complete example. I can compile the following code without any problems:

class vect
{
    float coeffs[4];
public:
    vect()
    {
        for (int k=0; k<4; ++k)
            coeffs[k] = 0;
    }

    vect(float x, float y, float z, float w)
    {
        coeffs[0] = x;
        coeffs[1] = y;
        coeffs[2] = z;
        coeffs[3] = w;
    }

    vect operator*(float scalar) const
    {
        return vect(
            scalar*coeffs[0],
            scalar*coeffs[1],
            scalar*coeffs[2],
            scalar*coeffs[3] );
    }
};

vect operator*(float scalar, vect const& x)
{
    return x*scalar;
}

void test()
{
    vect a (2,3,4,5);
    float s = 5;
    vect c, d;
    c = a * s;
    d = s * a;
}

So, the problem must lie somewhere else.

时光瘦了 2024-10-02 01:07:05

我还可以毫无问题地编译代码(就像 sellibitze 一样,他比我先一步!)

这是我使用的代码:

//main.cpp

#include <iostream>
using namespace std;


class Vect
{

public:
    float x,y,z,w;
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()
{
    x=y=z=w=0;
}

Vect operator*(const float p_sclr) const {
   return Vect( (x * p_sclr), (y * p_sclr), (z * p_sclr), 1); // reset w to 1
}


};

Vect operator*(const float p_sclr, const Vect& p_vect) {
   return p_vect * p_sclr;
}


int main()
{
    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
    cout << D.x << endl;
    return 0;
}

编辑:就像 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:

//main.cpp

#include <iostream>
using namespace std;


class Vect
{

public:
    float x,y,z,w;
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()
{
    x=y=z=w=0;
}

Vect operator*(const float p_sclr) const {
   return Vect( (x * p_sclr), (y * p_sclr), (z * p_sclr), 1); // reset w to 1
}


};

Vect operator*(const float p_sclr, const Vect& p_vect) {
   return p_vect * p_sclr;
}


int main()
{
    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
    cout << D.x << endl;
    return 0;
}

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?

挽心 2024-10-02 01:07:05

看起来 2 打败了我,但它也对我有用 - 构建并运行良好(VS2010,Win32 控制台项目):

class Vect
{
public:
    float x,y,z,w;
    Vect::Vect(){}
    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
    }
};

Vect operator*(const float p_sclr, const Vect& p_vect) {   return p_vect * p_sclr;}

int _tmain(int argc, _TCHAR* argv[])
{
    Vect a (2,3,4,5);
    float s = 5;
    Vect c, d;
    c = a * s;
    d = s * a;

}

Looks like 2 beat me to it, but it worked for me too - built and ran fine (VS2010, Win32 console project):

class Vect
{
public:
    float x,y,z,w;
    Vect::Vect(){}
    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
    }
};

Vect operator*(const float p_sclr, const Vect& p_vect) {   return p_vect * p_sclr;}

int _tmain(int argc, _TCHAR* argv[])
{
    Vect a (2,3,4,5);
    float s = 5;
    Vect c, d;
    c = a * s;
    d = s * a;

}
舟遥客 2024-10-02 01:07:05

最终的解决方案是使用友元函数,因为 p_vect 中的变量是私有的:

//class definition
friend Vect operator*(const float scale, const Vect& p_vect);

//function
Vect operator*(const float p_sclr, const Vect& p_vect) {
   return Vect( (p_vect.x * p_sclr), (p_vect.y * p_sclr), (p_vect.z * p_sclr), 1.0f);
}

The final solution was to go with a friend function because the variables were in p_vect were private:

//class definition
friend Vect operator*(const float scale, const Vect& p_vect);

//function
Vect operator*(const float p_sclr, const Vect& p_vect) {
   return Vect( (p_vect.x * p_sclr), (p_vect.y * p_sclr), (p_vect.z * p_sclr), 1.0f);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文