错误:无法应用运算符 [英] Error: Operator cannot be applied

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

问题描述

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


Erorr,我无能为力.如果您不能帮助我,其他http或电子邮件是否可以帮助我.谢谢!
请帮助我解决该错误.


Erorr, I cannot help myself. If you cannot to help me, is/are other http or email can to help me. THANKS!
Please help me in resolving the error.

推荐答案

该错误表示的内容是普通的英语.您需要定义一个知道如何添加int和类实例的运算符,因为默认情况下,编译器不知道如何执行此操作.向量类中有三个双打,而您想要做的就是将三个双打添加到单个int,这是您需要定义的事情.
The error means what it says, in plain english. You need to define an operator that knows how to add an int and an instance of your class, because by default, the compiler has no idea how to do that. There''s three doubles in your vector class, and what you want to do to add a group of three doubles to a single int, is something you need to define.


所有错误在于此部分:
All your errors lies in this part:
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?


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆