Click here to Skip to main content
15,892,805 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MSIL
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

C#
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, 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.
Posted
Updated 29-Jul-10 19:44pm
v3

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.
 
Share this answer
 
All your errors lies in this part:
C#
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:
C#
Console.WriteLine(vect1 = " vect1);
Console.WriteLine(vect2 = " vect2);
Console.WriteLine(vert3 = vert1 + vert2" + vect3);


Double quotes are missing. Try:
C#
Console.WriteLine("vect1 = " + vect1);
Console.WriteLine("vect2 = " + vect2);
Console.WriteLine("vert3 = vert1 + vert2 =" + vect3);


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?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900