Questions tagged [operator-overloading]

7

votes
2

answer
42

Views

Overloading operators for a class

Let's say I have the following class: class A { has $.val; method Str { $!val ~ 'µ' } } # Is this the right way of doing it? multi infix:(A:D $lhs, A:D $rhs) { ('(', $lhs.val, ',', $rhs.val, ')', 'µ').join; } How would I overload an operator (e.g., +) for a class in the same manner as Str in th...
uzlxxxx
1

votes
1

answer
50

Views

Can't overload i/o operators for private enum in namespace

I have a private enum in a class within a namespace. I'm trying to overload the I/O operators, but all I get is the compiler complaining about the Enum being private. The solution from this post did nothing to help me. Here is an isolated version of my problem. TestClass.h #include namespace Test {...
ABernitt
0

votes
1

answer
27

Views

C++ operator overloading [], where is the data parameter to assign?

I want to add a overload the operator [] in my class. Operator overloading is not something I've had to do before. I want to write an implementation to do the following: myclass a; a['test'] = 123; int test = a['test']; So far in my class the prototype looks like this: string operator[](const...
SPlatten
1

votes
1

answer
117

Views

Operator overloading on temporary

I'm having some problem trying to use bitshift operator on a temporary object returned by a method. The idea is to create a Log object as a temporary, and through the bitshift operator, append values which would be stored in a std::stringstream object. On the destruction of the temporary, the std::s...
pabloxrl
1

votes
1

answer
50

Views

std::min element weird behavior when class is wrapped by shared_ptr

I've had a long debugging session (6 hours or more). I was debugging my A* algorithm implementation. After checking every possibility, after adding logging, debugging step by step etc. I finally found the answer. Basically, it all boils down to one line, where I search for the minimum value in a vec...
dabljues
1

votes
2

answer
27

Views

Operator++ from CRTP base is not visible for compiler

Take following piece of code as an example: template struct base { Derived operator++(int){ auto tmp = static_cast(*this); ++static_cast(*this); return tmp; } }; struct der : public base { der &operator++(){ return *this; } }; int main(){ der d; d++;///
bartop
0

votes
1

answer
7

Views

Operator < doesn't seem to support “Any” type How to fix it?

The following is a quick-sort function written in Scala to sort a list of mixed types(int, double, float etc.). The error popped out and said in line 3 'Type mismatch, expected: T => Boolean, actual: T => Any Cannot resolve symbol
user132603
1

votes
0

answer
25

Views

Any way to highlight operators comparison for Objects in iOS Objective-C project

Recently in a iOS project I have refactored money data type from double to NSDecimalNumber to get a rid of discrepancies caused by handling money with doubles. The refactoring is complete but my concerns are for all those comparisons made through standard operators that are not raised as errors by t...
UrTe
1

votes
1

answer
70

Views

Operator overloading, definition/header for all operators

How best to split the following iterator code into a header and definition. // std::iterator example #include // std::cout #include // std::iterator, std::input_iterator_tag class MyIterator : public std::iterator { int* p; public: MyIterator(int* x) :p(x) {} MyIterator(const MyIterator& m...
Tom Shaw
1

votes
1

answer
39

Views

Using custom string class as map key value in c++

When I try to use my custom string class as the key value, I see this error undefined reference to `operator
Alex
1

votes
1

answer
39

Views

Overloading Operator + with two arrays c++

DynamicArray DynamicArray::operator+(const DynamicArray& rhs) const { int count = 0; int tempCapcacity = mCapacity; int newCapacity = mCapacity + rhs.mCapacity; string *temp = allocateAndCopyToNewArray(mWords, mNumWords, newCapacity); for (int i = tempCapcacity; i < newCapacity; i++) { temp[i] = rhs...
Aidan Smith
1

votes
0

answer
33

Views

Is there any way of extending the Point (or any other) type with overloaded operators?

In a loop within an OnPaint event, I draw a string using DrawString. Before drawing the string, the client rectangle oRect needs to be offset by some pixels. Being in a loop, it is clear that this offsetting must be undone, or they will add up. This works as intended: Protected Overrides Sub OnPaint...
Herb
1

votes
0

answer
121

Views

Overloading algebraic operators in CRTP class as free functions and return common type

my objective is to write a vector and matrix class. For the matrix class I need a specialization for the 2D case. So I decided to inherit specialized classes from a MatrixBase class. What I also want is easy implementation of generic algebra with all derived matrix classes. To be more precise: I wan...
Nyquist
1

votes
1

answer
93

Views

overloading operator<< undefined reference

This is my h file: class Student { public: Student(std::string _name, size_t _age) :m_name(_name), m_age(_age){} friend std::ostream& operator
Hana
1

votes
0

answer
66

Views

Matlab: Why is full/sparse matrix addition slower than full/full matrix addition?

Why is adding a sparse and a full matrix slower than adding two full matrices? The following code demonstrates consistent superior performance for hFullAddFull. I_FULL = 600; J_FULL = 10000; FULL_COUNT = I_FULL*J_FULL; NON_ZERO_ELEMENT_COUNT = 1000; nonZeroIdxs = randsample(FULL_COUNT, NON_ZERO_ELEM...
rnoodle
1

votes
0

answer
45

Views

Getting compiler error 'binary '<<' : no operator found' in VS 2005

I am getting the below compiler error when I build my project: XMLHelper.h(22) : error C2678: binary '
Raghu Ramaiah
1

votes
0

answer
42

Views

Generic Single Linked List, No Viable overloaded =

I am creating a generic single linked list for the first time. I have this error that states: No viable overloaded '=' So I decided to overload the '=' operator to make it work but I am still receiving the same error and I am not sure why. Here is my header file: #ifndef LinkedList_hpp #define Linke...
Snorrlaxxx
1

votes
1

answer
59

Views

How to generate Roslyn Operator Overloads

I want to leverage Roslyn code generation to generate a Unary Operator and several Binary Operators but I was wondering how I could go about doing that. Assumptions, Descriptor carries with it information such as the underlying TypeSyntax for which I am declaring the operators. So far I've postulate...
mwpowellhtx
1

votes
1

answer
93

Views

C# operator overloading for matrix

I have written a code for operator overloading for matrix as below. But the result for multiplication with a negative sign doesn't work well. public static MyMatrix operator -(MyMatrix A, MyMatrix B) { MyMath math = new MyMath(); return math.matSubtract(A, B); } public static MyMatrix operator -(MyM...
user9200689
3

votes
0

answer
39

Views

operator< gives operator __surrogate_func error

I've implemented operator< for my c++ classes and when I tried it for a class that contains object vector; it fails and gives operator __surrogate_func When I remove object vector from class, everything is OK. I found this similar question about question but I dont know why Im getting this error? Be...
yunus celik
1

votes
1

answer
60

Views

Operator overload with a reference return in C++ class

What's the difference between this two function? double &operator[](size_t i) { return features_[i]; } double operator[](size_t i) const { return features_[i]; } 1, the first one allows to modify the features_[i] but the second not? 2, which operator will be chosen when I write Mytype[i] = 0 and dou...
Patrick
1

votes
2

answer
59

Views

Operator overloading using deep copy in c++

#include #include #include using namespace std; class String { char *value; int len; public: String() { len=0; value=0; } ~String() {} String(char *s) { len=strlen(s); value=new char[len+1]; strcpy(value,s); } String(String & s) { len=s.len; value=new char[len+1]; strcpy(value,s.value); } friend S...
1

votes
1

answer
41

Views

Overload the stream insertion operator for nested structures/classes

I want to overload the stream insertion operator for a structure nested within a class. How can I fix this error and make the function to work, or is there any alternative method to implement it? struct S { int a; int b; }; class T { private: S** arrayName; int r; int c; public: friend ostream& oper...
Uncle Marvin
1

votes
1

answer
36

Views

Overload operator* to get reference to instance of another class

I have classes class Square { public: virtual void apply(Player*) = 0; //apply square effect to the player }; class BoardIterator { public: BoardIterator(); Square &operator*();//return current square where player is Square &operator+(int);//move forward certain number of squares private: static con...
Ilya Busov
1

votes
1

answer
25

Views

Operator overloading for array of elements

I have a built a class and would like to add Operator Overloading for minus. I wrote the code and it works just fine if I input only one element, however the function I wrote is supposed to work for multiple elements. When I try to input an array, (array of elements)-(one element), it should 'deduct...
gutzcha
1

votes
0

answer
52

Views

Overloading assignment operators in proxy class for matrix and vector?

I have a slight problem. I have a Matrix class defined as follows (in row-major form): template class Matrix { // must be inline friend std::ostream &operator
Jonathan Lee
1

votes
1

answer
66

Views

How to do operator overloading between two different classes in python

Consider the main class class point2D: def __init__(x, y): self.x = x self.y = y def __sub__(self, other): return vector2D(self.x - other.x, self.y - other.y) and the subclass: class vector2D(point2D): def __add__(self, other): return vector2D(self.x + other.x, self.y + other.y) now I want the + ope...
Foad
1

votes
0

answer
37

Views

Python, Class, Call function inside the class, Operator Overloading (Matrix Multiplication)

I am trying to write a class that has a mutiplication operator overloading function that is used to handle matrix multiplcation. My method is Transpose a matrix(I write a transpose function inside the class) which is called T Write a dot product function which handle the mutiplication for Row A an...
MOSOON-E
1

votes
2

answer
27

Views

C++ overloading operator, compiler doesn't see the operator

CMagazin.h class CMagazin { char *m_nume; listList_produse; public: void printExpirabile( const char* data); ~CMagazin(); }; CMagazin.cpp void CMagazin::printExpirabile(const char *xdata) { list::iterator it; for (it = List_produse.begin(); it != List_produse.end(); ++it) { CProdus* p = *it; if (p->...
Andrei Alexandru
1

votes
1

answer
26

Views

I want to give an input of adding two numbers but it should return multiplication of those two numbers using operator overloading

I am a newbie. I want to use operator overloading which gives 3+4 but returns answer of 3*4 I have made a class and passed two functions add and mul class A: def __init__(self, a,b): self.a = a self.b = b # adding two objects def __add__(self, other): return self.a + other.a , self.b + other...
Sca
1

votes
1

answer
37

Views

Operator overloading errors for a simple 3x3 matrix class

So I have this as my class.cpp File #include 'matrixType3.h' #include #include #include using namespace std; void matrixType3::setElement(int row, int col, int newvalue) { matrix[row][col] = newvalue; } double matrixType3::getElement(int row, int col) { int j = matrix[row][col]; return j; } void...
Derek Langer
1

votes
0

answer
25

Views

Replacing Implicitly-Deleted Operator With Template Method [duplicate]

This question already has an answer here: Template assignment operator doesn't replace the default assignment operator 2 answers Template assignment operator overloading mystery 2 answers Consider the following code: class Foo { public: int const i; Foo() : i(0) {} }; void test() { Foo a; Fo...
imallett
1

votes
0

answer
42

Views

Returned type in operator overloading

Suppose to have the following class definition: template class Foo { public: T x; Foo(T x): {x = 1;} } And to overload the following functions: // function 1 Foo operator*(T s) const { return Foo(x*s); } // function 2 Foo &operator*=(T s) { x *= s; return *this; } Now, i instantiate an object: Foo...
Neb
1

votes
0

answer
21

Views

Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)

I'm having problems with my friend function within my template class. For some reason it doesn't like the fact that I'm trying to use a variable that is type T in an operator overloading friend function. #include #include #include template class LL { struct Node { T mData; Node *mNext; Node(); N...
Augrunga
1

votes
1

answer
22

Views

Overloading '+' operator in spark

What's the way to go about overloading operators via spark ? Like if i enter, col1 + col2 in some text box (and col1, col2 are actual columns in dataset) and then pass that info to my api, then depending on col1 and col2 either spark's concat or plus should be called. I've tried writing an UDF to do...
Ajinkya Jumbad
1

votes
3

answer
382

Views

function overloading fail: why did these operators clash?

I've got a big big code base that includes two main namespaces: the engine and the application. The engine defines a vector3 class as a typedef of another vector3 class, with equality operators that sit in the engine namespace, not in the vector3 class. I added a class to the application that als...
tenpn
1

votes
2

answer
891

Views

Can I define a Perl-like binding operator (=~) in C#?

I really like being able to use =~ and !~ in Perl to evaluate a string against a regular expression. I'd like to port this functionality over to C#, but it appears that, while you can overload operators, you can't create new ones. I'm considering extending the string type to provide a Match() method...
Tequila Jinx
1

votes
1

answer
168

Views

Do I need to provide cast methods for cast operator overloads in C#, to allow people to use in VB

I read somewhere that older VB .net (pre VB .NET 2005?) couldn't use overloaded operators in a C# class. Is it true for overloaded cast operators? Either way do you guys think it's a disadvantage for a C# class library not to include them and arithmetic methods as actual methods for other languages,...
Joan Venge
0

votes
1

answer
32

Views

Operator[] overloading returns wrong data

I've followed the operator overloading tutorial from TutorialsPoint. This is my class: struct Decimal { private: int16_t exponent; uint8_t length; uint16_t mantissa[DecimalMaxSize]{}; ... public: uint16_t &operator[](int i); ... and my implementation: uint16_t &Decimal::operator[](int i) { if( i >...
Christian Seiler

View additional questions