Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C# Expert!

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




return type

 
Reply to this topicStart new topic

return type

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

New D.I.C Head
*

Joined: 16 Sep, 2008
Posts: 21



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
+Quote Post


AmitTheInfinity
RE: Return Type
1 Oct, 2008 - 07:10 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,258



Thanked: 64 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
+Quote Post

eclipsed4utoo
RE: Return Type
1 Oct, 2008 - 07:15 AM
Post #3

D.I.C Lover
Group Icon

Joined: 21 Mar, 2008
Posts: 1,165



Thanked: 117 times
Dream Kudos: 125
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
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:31AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month