Code: -
using System;
-
-
namespace n_overload1
-
{
-
class Vector
-
{
-
int x, y, z;
-
-
public Vector(int a, int b, int c)
-
{
-
x = a;
-
y = b;
-
z = c;
-
}
-
-
public static Vector operator + (Vector a, Vector b)
-
{
-
Vector c1;
-
c1.x = a.x + b.x;
-
c1.y = a.y + b.y;
-
c1.z = a.z + b.z;
-
return (c1);
-
}
-
-
public void Display()
-
{
-
Console.Write(x + " " + y + " " + z);
-
Console.WriteLine();
-
}
-
-
}
-
-
-
-
-
-
-
class Class1
-
{
-
static void Main(string[] args)
-
{
-
Vector V1,V2,V3;
-
V1 = new Vector(5,5,5);
-
V2 = new Vector(2,2,2);
-
-
V3 = V1 + V2;
-
V3.Display();
-
-
Console.ReadLine();
-
-
-
}
-
}
-
}
-
-
Code:
error:
use of unassigned local variable c1 near this c1.x = a.x + b.x; 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
Also, you are going to want doubles, floats, or decimels for your values when dealing with vectors
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
You never called a constructor for Vector C1.
Gotta do that.
You never called a constructor for Vector C1.
Gotta do that.
Darn double thread made it get rid of my response to that effect
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.
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. -
using System;
-
-
namespace n_overload1
-
{
-
class Vector
-
{
-
int x, y, z;
-
-
public Vector(int a, int b, int c)
-
{
-
x = a;
-
y = b;
-
z = c;
-
}
-
-
public static Vector operator + (Vector a, Vector b)
-
{
-
Vector c1 = new Vector();
-
c1.x = a.x + b.x;
-
c1.y = a.y + b.y;
-
c1.z = a.z + b.z;
-
return (c1);
-
}
-
-
public void Display()
-
{
-
Console.Write(x + " " + y + " " + z);
-
Console.WriteLine();
-
}
-
-
}
-
-
-
class Class1
-
{
-
static void Main(string[] args)
-
{
-
Vector V1,V2,V3;
-
V1 = new Vector(5,5,5);
-
V2 = new Vector(2,2,2);
-
V3 = new Vector();
-
-
V3 = V1 + V2;
-
V3.Display();
-
-
Console.ReadLine();
-
-
-
}
-
}
-
}
-
pls pls pls help me!
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: -
class Vector
-
{
-
int x, y, z;
-
-
public Vector()
-
{//default constructor
-
x=0;
-
y=0;
-
z=0;
-
}
-
-
public Vector(int a, int b, int c)
-
{
-
x = a;
-
y = b;
-
z = c;
-
}
-
-
public static Vector operator + (Vector a, Vector b)
-
{
-
Vector c1 = new Vector();
-
c1.x = a.x + b.x;
-
c1.y = a.y + b.y;
-
c1.z = a.z + b.z;
-
return (c1);
-
}
-
-
public void Display()
-
{
-
Console.Write(x + " " + y + " " + z);
-
Console.WriteLine();
-
}
-
}
-
OR, fixing your constructor: -
public static Vector operator + (Vector a, Vector b)
-
{
-
Vector c1 = new Vector( (a.x + b.x), (a.y + b.y), (a.z + b.z) );
-
return (c1);
-
}
-
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: -
class Vector
-
{
-
int x, y, z;
-
-
public Vector()
-
{//default constructor
-
x=0;
-
y=0;
-
z=0;
-
}
-
-
public Vector(int a, int b, int c)
-
{
-
x = a;
-
y = b;
-
z = c;
-
}
-
-
public static Vector operator + (Vector a, Vector b)
-
{
-
Vector c1 = new Vector();
-
c1.x = a.x + b.x;
-
c1.y = a.y + b.y;
-
c1.z = a.z + b.z;
-
return (c1);
-
}
-
-
public void Display()
-
{
-
Console.Write(x + " " + y + " " + z);
-
Console.WriteLine();
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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>
|
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...
|
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...
|
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
|
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...
|
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...
|
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:
|
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 &...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |