Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,288 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,246 people online right now. Registration is fast and FREE... Join Now!




return type

 
Reply to this topicStart new topic

return type

SleepingOlive
post 1 Oct, 2008 - 06:57 AM
Post #1


New D.I.C Head

*
Joined: 16 Sep, 2008
Posts: 20



Thanked 1 times
My Contributions


i am trying to write a program to overload the ^ operator. so far when i compile it i have two of the same errors on lines 10 and 17. The compiler is telling me that the struct, class, or interface must have a return type. I thought that I was returning in in line 40 but there is some other mistake that I am not catching.
here is the code!?

CODE

using System;

class power
{
    public int x;//, y, z;
    
    public Vector(int x)//, int y, int 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 * rhs.x * rhs.x;
        //result.y = rhs.y * rhs.y * rhs.y;
        //result.z = rhs.z * rhs.z * rhs.z;
        return result;
    }
    
    public static Vector operator ^ (Vector lhs, Vector rhs)
    {
        return new Vector(lhs * rhs.x);
    }
    
} //END CLASS POWER

class RaisePower
{
    public static void Main(string[] args)
    {
        Vector vect1;
        vect1 = new Vector(3.0);
        
        Console.WriteLine("vect1 = " + vect1.ToString());
    }
}
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 1 Oct, 2008 - 07:10 AM
Post #2


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


well your class name is power and you wrote constructors with name Vector smile.gif
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 1 Oct, 2008 - 07:15 AM
Post #3


D.I.C Regular

Group Icon
Joined: 21 Mar, 2008
Posts: 314



Thanked 17 times

Dream Kudos: 25
My Contributions


you can't overload methods like that. you have the exact same signature for both methods.

so when you call the "operator" method with two vector parameters, which method should it use? both are the same so how is the compiler suppose to know which to use?

also, you don't need to use the "new" in the second "operator" method.

and also as AmitTheInfinity pointed out, your class name and your constructor names are different. Since they are different, the compiler is assuming they are regular methods....which you you aren't specifying a return value for either.

This post has been edited by eclipsed4utoo: 1 Oct, 2008 - 07:16 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 01:13AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month