本文介绍了错误:无法应用运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Error   1   Operator cannot be applied to operands of type int and cpp_Professional159.Program.Vector   C:\Documents and Settings\Eddy Ho\Local Settings\Application Data\Temporary Projects\cpp-Professional159\Program.cs 67  46  cpp-Professional159
Error   2   Operator cannot be applied to operands of type cpp_Professional159.Program.Vector and cpp_Professional159.Program.Vector   C:\Documents and Settings\Eddy Ho\Local Settings\Application Data\Temporary Projects\cpp-Professional159\Program.cs 68  13  cpp-Professional159
Error   3   Operator cannot be applied to operands of type cpp_Professional159.Program.Vector  C:\Documents and Settings\Eddy Ho\Local Settings\Application Data\Temporary Projects\cpp-Professional159\Program.cs 70  21  cpp-Professional159
Error   4   Cannot implicitly convert type cpp_Professional159.Program.Vector C:\Documents and Settings\Eddy Ho\Local Settings\Application Data\Temporary Projects\cpp-Professional159\Program.cs 72  26  cpp-Professional159
Error   5   Cannot implicitly convert type to cpp_Professional159.Program.Vector C:\Documents and Settings\Eddy Ho\Local Settings\Application Data\Temporary Projects\cpp-Professional159\Program.cs 40  24  cpp-Professional159


using System;
using System.Collections.Generic;
using System.Text;

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 return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.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;
                return result;
            }
            */
            public static Vector operator * (Vector lhs, Vector rhs)
            {
                return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
            }

        }


        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(vert3 = vert1 + vert2" + vect3);
            Console.WriteLine("2*vect3 = ", 2*vect3);
            vect3 += vect2;
            Console.WriteLine("vect3+=vect2 gives " , vect3);
            vect3 = vect1*2;
            Console.WriteLine("Setting vect3 = vect1 * 2" , vect3);
            double dot = vect1*vect3;
            Console.WriteLine("vect1 * vect3 = ", dot);
        }
    }
}



请帮助我解决该错误.



Please help me in resolving the error.

推荐答案


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(vert3 = vert1 + vert2" + vect3);
            Console.WriteLine("2*vect3 = ", 2*vect3);
            vect3 += vect2;
            Console.WriteLine("vect3+=vect2 gives " , vect3);
            vect3 = vect1*2;
            Console.WriteLine("Setting vect3 = vect1 * 2" , vect3);
            double dot = vect1*vect3;
            Console.WriteLine("vect1 * vect3 = ", dot);
        }



尽管您尚未告诉我们弹出错误的行,但看起来像是这些行:



Though you have not told us the lines that is popping the error, but it looks like these are the one:

Console.WriteLine(vect1 = " vect1);
Console.WriteLine(vect2 = " vect2);
Console.WriteLine(vert3 = vert1 + vert2" + vect3);



双引号丢失.试试:



Double quotes are missing. Try:

Console.WriteLine("vect1 = " + vect1);
Console.WriteLine("vect2 = " + vect2);
Console.WriteLine("vert3 = vert1 + vert2 =" + vect3);



然后还有更多:
您正在尝试:vect3 = vect1*2;
您在哪里定义Vector对象可以与Integer相乘?

此外,在将两个向量相乘之后,您尝试将其成倍增加.您在哪里定义/完成了矢量乘法?



Then there are more:
You are trying: vect3 = vect1*2;
Where have you defined that a Vector object can be multiplied by an Integer?

Further, after multiplying two vectors you are trying to get that in a double. Where have you defined/done that for vector multiplication?


这篇关于错误:无法应用运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:33