本文介绍了2-//错误-vect3 * 2 = AND vect3 = vect1 * 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在这里出错.
2-//错误-vect3 * 2 = AND vect3 = vect1 * 2

I am getting error here.
2 - //Error - vect3 * 2 = AND vect3 = vect1 * 2

namespace cpp_Professional159
{
    class Program
    {
        struct Vector
        {
            public double x, y, z;
            public Vector(double x, double y, double z)
            {
                this.x = x;
                this.y = y;
                this.z = z;
            }
            public Vector(Vector rhs)
            {
                x = rhs.x;
                y = rhs.y;
                z = rhs.z;
            }
            public override string ToString()
            {
                return "( " x + "," + y + "," + z "  )";
            }
            public static Vector operator + (Vector lhs, Vector rhs)
            {
                Vector result = new Vector(lhs);
                result.x += rhs.x;
                result.y += rhs.y;
                result.z += rhs.z;
                Console.WriteLine("Vector operator + (Vector lhs, Vector rhs)");
                return result;
            }
            public static double operator *(Vector lhs, Vector rhs)
            {
                return rhs.x * lhs.x + rhs.y * lhs.y + rhs.z * lhs.z;
                //return lhs * rhs;
            }
            /*
            public static double operator * (Vector lhs, Vector rhs)
            {
                //return new Vector(rhs * lhs.x, rhs * lhs.y, rhs * lhs.z);
                return lhs*rhs;
            }*/
        }
        /*
        static void Main(string[] args)
        {
            Vector vect1, vect2, vect3;
            vect1 = new Vector(1.0, 1.5, 2.0);
            vect2 = new Vector(0.0, 0.0, -10.0);
            vect3 = vect1 + vect2;
            Console.WriteLine("vect1 = " + vect1.ToString());
            Console.WriteLine("vect2 = " + vect2.ToString());
            Console.WriteLine("vect3 = " + vect3.ToString());
        }
        */
        ///*
        static void Main(string[] args)
        {
            Vector vect1, vect2, vect3;
            vect1 = new Vector(1.0, 1.5, 2.0);
            vect2 = new Vector(0.0, 0.0, -10.0);
            vect3 = vect1 + vect2;
            Console.WriteLine("vect1 = " + vect1);
            Console.WriteLine("vect2 = " + vect2);
            Console.WriteLine("vect3 =  vert1 + vect2 = " + vect3);
            Console.WriteLine("vect3 * 2 = " + vect3*2);   //Error
            vect3 += vect2;
            Console.WriteLine("vect3 += vert2 = " + vect3);
            vect3 = vect1*2;                               //Error
            Console.WriteLine("Setting vect3 = vect1 * 2 gives "  + vect3);
            double dot = vect1*vect3;
            Console.WriteLine("vect1 * vect3 = " + dot);
        }

    }
}

推荐答案

public static double operator *(Vector lhs, Vector rhs)            {
    return rhs.x * lhs.x + rhs.y * lhs.y + rhs.z * lhs.z;   //return lhs * rhs;            
}



当您执行vect3 * 2 vect1 * 2时,您试图将乘法运算符与一个向量和一个整数一起使用.您尚未处理这种情况.您还需要重载运算符来处理这种情况.

系统对它们进行* 2运算时,系统本身无法理解是否需要将向量加倍.



When you do vect3 * 2 OR vect1 * 2, you are trying to use multiplication operator with ONE vector and ONE Integer. You have NOT handled such scenario. You need to overload operator to handle such scenario too.

System cannot understand by itself that it needs to double the vector when you do *2 with them.



这篇关于2-//错误-vect3 * 2 = AND vect3 = vect1 * 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 22:14