473,421 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

simple overloading of binary operator +

Code:

Expand|Select|Wrap|Line Numbers
  1. using System; 
  2.  
  3. namespace n_overload1 
  4. class Vector 
  5. int x, y, z; 
  6.  
  7. public Vector(int a, int b, int c) 
  8. x = a; 
  9. y = b; 
  10. z = c; 
  11.  
  12. public static Vector  operator + (Vector a, Vector b) 
  13. Vector c1;        
  14. c1.x = a.x + b.x; 
  15. c1.y = a.y + b.y; 
  16. c1.z = a.z + b.z; 
  17. return (c1); 
  18.  
  19. public void Display() 
  20. Console.Write(x + " " + y + " " + z); 
  21. Console.WriteLine(); 
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. class Class1 
  30. static void Main(string[] args) 
  31. Vector V1,V2,V3; 
  32. V1 = new Vector(5,5,5); 
  33. V2 = new Vector(2,2,2); 
  34.  
  35. V3 = V1 + V2; 
  36. V3.Display(); 
  37.  
  38. Console.ReadLine(); 
  39.  
  40.  
  41.  
  42.  
Code:

error:

use of unassigned local variable c1 near this c1.x = a.x + b.x;
Aug 10 '07 #1
9 1222
hey guys,
plz help me by replying my thread. it is on operator overloading of + operator. look in the links , plz help me
Aug 10 '07 #2
TRScheel
638 Expert 512MB
Also, you are going to want doubles, floats, or decimels for your values when dealing with vectors
Aug 10 '07 #3
TRScheel
638 Expert 512MB
hey guys,
plz help me by replying my thread. it is on operator overloading of + operator. look in the links , plz help me
Answered, though next time, just reply to the original thread
Aug 10 '07 #4
Plater
7,872 Expert 4TB
You never called a constructor for Vector C1.
Gotta do that.
Aug 10 '07 #5
TRScheel
638 Expert 512MB
You never called a constructor for Vector C1.
Gotta do that.
Darn double thread made it get rid of my response to that effect
Aug 10 '07 #6
You never called a constructor for Vector C1.
Gotta do that.
hey i did not get that. could u plz inculcate that in the code cos i am so dumb. actually i don't agree with u fully cos v3=v1+v2 and how can i initiliaze the constructor c1. i am all confused. i would highly appreciate yr reply. thanking u in anticipation.
Aug 10 '07 #7
You never called a constructor for Vector C1.
Gotta do that.
hey Plater,
now i got yr point, late realization. but even after implementing what u said , it is giving me teh error "No overload for method Vector takes 0 arguments" i am posting the code here with the changes implemented.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. namespace n_overload1
  4. {
  5.     class Vector
  6.     {
  7.         int x, y, z;
  8.  
  9.         public Vector(int a, int b, int c)
  10.         {
  11.             x = a;
  12.             y = b;
  13.             z = c;
  14.         }
  15.  
  16.         public static Vector  operator + (Vector a, Vector b)
  17.         {
  18.             Vector c1 = new Vector();
  19.             c1.x = a.x + b.x;
  20.             c1.y = a.y + b.y;
  21.             c1.z = a.z + b.z;
  22.             return (c1);
  23.         }
  24.  
  25.         public void Display()
  26.         {
  27.             Console.Write(x + " " + y + " " + z);
  28.             Console.WriteLine();
  29.         }
  30.  
  31.     }
  32.  
  33.  


Expand|Select|Wrap|Line Numbers
  1.     class Class1
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.          Vector V1,V2,V3;
  6.          V1 = new Vector(5,5,5);
  7.          V2 = new Vector(2,2,2);
  8.          V3 = new Vector();
  9.  
  10.             V3 = V1 + V2;
  11.             V3.Display();
  12.  
  13.             Console.ReadLine();
  14.  
  15.  
  16.         }
  17.     }
  18. }
  19.  
pls pls pls help me!
Aug 10 '07 #8
Plater
7,872 Expert 4TB
You do not have a default constructor for a vector:
You must either instanciate with the constructor overload you do have, or create the default one.

Adding the default constructor:
Expand|Select|Wrap|Line Numbers
  1. class Vector
  2. {
  3.     int x, y, z;
  4.  
  5.     public Vector()
  6.     {//default constructor
  7.       x=0;
  8.       y=0;
  9.       z=0;
  10.     }
  11.  
  12.     public Vector(int a, int b, int c)
  13.     {
  14.         x = a;
  15.         y = b;
  16.         z = c;
  17.     }
  18.  
  19.     public static Vector  operator + (Vector a, Vector b)
  20.     {
  21.         Vector c1 = new Vector();
  22.         c1.x = a.x + b.x;
  23.         c1.y = a.y + b.y;
  24.         c1.z = a.z + b.z;
  25.         return (c1);
  26.     }
  27.  
  28.     public void Display()
  29.     {
  30.         Console.Write(x + " " + y + " " + z);
  31.         Console.WriteLine();
  32.     }
  33. }
  34.  
OR, fixing your constructor:
Expand|Select|Wrap|Line Numbers
  1. public static Vector operator + (Vector a, Vector b)
  2. {
  3.    Vector c1 = new Vector( (a.x + b.x), (a.y + b.y), (a.z + b.z) );
  4.    return (c1);
  5. }
  6.  
Aug 10 '07 #9
thanks platter, u are a genius. bless u


You do not have a default constructor for a vector:
You must either instanciate with the constructor overload you do have, or create the default one.

Adding the default constructor:
Expand|Select|Wrap|Line Numbers
  1. class Vector
  2. {
  3.     int x, y, z;
  4.  
  5.     public Vector()
  6.     {//default constructor
  7.       x=0;
  8.       y=0;
  9.       z=0;
  10.     }
  11.  
  12.     public Vector(int a, int b, int c)
  13.     {
  14.         x = a;
  15.         y = b;
  16.         z = c;
  17.     }
  18.  
  19.     public static Vector  operator + (Vector a, Vector b)
  20.     {
  21.         Vector c1 = new Vector();
  22.         c1.x = a.x + b.x;
  23.         c1.y = a.y + b.y;
  24.         c1.z = a.z + b.z;
  25.         return (c1);
  26.     }
  27.  
  28.     public void Display()
  29.     {
  30.         Console.Write(x + " " + y + " " + z);
  31.         Console.WriteLine();
  32.     }
  33. }
  34.  
Aug 10 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Stub | last post by:
When I try to overload the == operator, it gives me an "error C2804: binary 'operator ==' has too many parameters." bool operator==(const Store& Store1, const Store& Store2); After Adding...
6
by: c++newbie | last post by:
Hi all, I try to compile the following classes: main.cpp: #include <algorithm> #include <iostream> #include <fstream> #include <iterator>
13
by: Jon Cosby | last post by:
VB .Net does support operator overloading, doesn't it? It seems like this should overload binary addition, but VB doesn't recognize "Operator" Public Shared Operator +(ByVal c1 as cnum, ByVal c2...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
10
by: looping | last post by:
Hi, for the fun I try operator overloading experiences and I didn't exactly understand how it works. Here is my try: def __pow__(self, value): return self.__add__(value) 6
19
by: Jess | last post by:
Hello, After seeing some examples about operator overloading, I'm still a bit confused about the general syntax. The following is what I think, not sure whether it's correct. 1. For a unary...
2
by: Bharath | last post by:
Hello All, Can you please let me know if we can do pointer arthrmetic using operator overloading? If not, can you please explain why it's not supported by compiler? I tried below e.g. which was...
8
by: Skybuck Flying | last post by:
Hello, Visual Studio .Net 2005 (Win32) Compile error: Error 1 error C2804: binary 'operator +' has too many parameters <snipped> line 16 class TSkybuckInt32 { private:
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
2
by: jimzat | last post by:
I am trying to simulate the execution of some PLC ladder logic in python. I manually modified the rungs and executed this within python as a proof of concept, but I'd like to be able to skip the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.