【问题标题】:Binary * Operator Not Found未找到二进制 * 运算符
【发布时间】:2010-09-28 03:28:05
【问题描述】:

我有一个带有正确重载的 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 的帮助不是很大。这是我收到的唯一编译错误或警告。

【问题讨论】:

  • 你确定吗?我希望A * s 不工作,s * A 工作......
  • 请发布一个完整的简短示例,以便大家了解 Vect 的定义方式和位置,以及您在哪个命名空间中定义了运算符*
  • 您没有在右花括号后用分号标记 Vect 类的结尾。这是复制粘贴错误还是您的代码实际看起来像这样?
  • 复制粘贴,现在编辑。

标签: c++ visual-studio-2008 operator-overloading


【解决方案1】:

你还没有发布一个完整的例子。我可以毫无问题地编译以下代码:

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;
}

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

【讨论】:

    【解决方案2】:

    我也可以毫无问题地编译代码(比如sellebitze,谁打败了我!)

    这是我使用的代码:

    //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?

    【讨论】:

      【解决方案3】:

      看起来 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;
      
      }
      

      【讨论】:

        【解决方案4】:

        最终的解决方案是使用友元函数,因为 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);
        }
        

        【讨论】:

        • 如何解决原来的问题?为什么您认为 operator*(float,Vect) 的稍微不同的实现会突然使编译器以不同的方式查找运算符?下次只提供一个完整的例子,包括真正的错误信息。
        • 它解决了原来的问题,因为编译器现在看到该函数存在。不知道为什么它不是放在首位。发布一个完整的示例意味着复制文件的 600 多行代码,我无法通过另一个小的从头开始的测试来复制这个问题。
        猜你喜欢
        • 1970-01-01
        • 2016-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多