2 Replies - 954 Views - Last Post: 01 October 2008 - 08:15 AM Rate Topic: -----

#1 SleepingOlive  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 16-September 08

return type

Post icon  Posted 01 October 2008 - 07:57 AM

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!?

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



Is This A Good Question/Topic? 0
  • +

Replies To: return type

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: return type

Posted 01 October 2008 - 08:10 AM

well your class name is power and you wrote constructors with name Vector :)
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: return type

Posted 01 October 2008 - 08:15 AM

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: 01 October 2008 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1