operator-overloading

C++ thinks '<<' is not a member of class, but it is

Hello. I have to write a simple log class, that would write the output to a file. I want it to work with overloading the << operator, so I can do this: MyLog log("C:\\log.txt"); log<<"Message"; But Visual C++ tells me: "error C2039: '<<' : is not a member of 'MyLog' " I don't know what I am doing wrong. Here is the code: MyLog.h ...

Automatically deducing operators in C++?

Is it possible in C++ for the compiler/language to automatically deduce unimplemented operators? For example, if I have: class X { public: bool operator ==(const X &x) const; }; Is there a way for != to be deduced implicitly? And I'll exploit this questions for a semi-related one: How come map's only requirement from it's keys is...

C++ XML library heavily utilizing operator overloading

Some time ago I saw a XML library for C++ which heavily utilized operator overloading, allowing for cute syntax similar to the following: #include <iostream> #include <some_xml_library/some_header.hpp> using namespace some_xml_library; int main() { elem_t div; doc_t d = _ <div>"hello"<!div> _; std::cout << d; } Output: ...

c++ overload operator==

I have a class with the following bool DistinctWord::operator==(const DistinctWord W) const { return strWord == W.strWord; } bool DistinctWord::operator==(const DistinctWord& W) const { return strWord == W.strWord; } I'm doing this in my program DistinctWord* wordOne = new DistinctWord("Test"); DistinctWord* wordTwo ...

How to overload opAssign operator "globally" in C++

Just curious about how to overload them. The opAssign operators are like addAssign(+=) and subAssign(-=). "globally" means they are not overloaded as member functions, but just a operator act on operands For these opAssign operators, they are binary operators.(they receive two operands) Therefore two parameters are needed. I found n...

Multiple output operators?

Hi, is it possible to define multiple output operators for an enum? I want to use this std::ostream& operator<< (std::ostream& os, my_enum e); operator to (1) print a human readable text and to (2) convert it to some code for storing in a database. Thanks ...

Operator Overloading in Clojure

Even looking closely over documentation on Clojure, I do not see any direct confirmation as to whether or not Clojure supports operator overloading. If it does, could someone provide me with a quick snipplet of how to overload, let's say, the "+" operator to delegate to some predefined method that we can call myPlus. I am very new to C...

Implement math functions in custom C# type?

Could someone point me to the interface that I need to implement in order to get basic math operators (i.e. +, -, *, /) to function on a custom type? ...

C++ - Using std::count() with abstract data types ?

Hello, My code is using std::count() on a list of an abstract data type that i have defined. (Sommet or Edge in english). But it doesn't work, although i've overloaded the < and == operators like this : bool operator< (const Sommet &left, const Sommet &right) { if(left.m_id_sommet < right.m_id_sommet) return true; return fals...

Rules of thumb for when to use operator overloading in python

From what I remember from my C++ class, the professor said that operator overloading is cool, but since it takes relatively a lot of thought and code to cover all end-cases (e.g. when overloading + you probably also want to overload ++ and +=, and also make sure to handle end cases like adding an object to itself etc.), you should only c...

Problem overloading the < operator in C++

I have a vector of Student objects which I want to sort using #include <algorithm> and sort(list.begin(), list.end()); In order to do this, I understand that I need to overload the "<" operator but after trying (and failing) with several methods suggested online, I am running out of ideas. Here is my latest attempt: In Student.h... ....

Operator Overload << in Linked List

How can I overload the operator <<. The purpose of the overloaded operator is to do: cout << ptr->info and not receive the memory address but Display the maker year and model of that node's info section. Example: template <class DataType> struct Node { DataType info; Node<DataType> *next; }; In each info section of the Node there wil...

C++ Overloading << Operator problem

I'm fairly noobish at C++, but very comfortable with pointers, dereferencing, etc. I'm having a problem with my overload of the << operator for a class, in that it compiles fine but crashes when run. It feels like an infinite loop, but I'm not certain. Here's the code, and any help is appreciated. #include <string> #include <iostream> ...

What's the ampersand for when used after class name like ostream& operator <<(...)?

I know about all about pointers and the ampersand means "address of" but what's it mean in this situation? Also, when overloading operators, why is it common declare the parameters with const? ...

Is it a bad thing to override == and have an instance compare equal to null ?

Background Let's say I have the following class: public class MyValue<T> { public T Value { get; set; } public static bool operator ==(MyValue<T> first, MyValue<T> second) { // if first and second are the same instance, they are equal if (object.Equals(first, second)) { return true; ...

How do I overload the in operator in Groovy?

def array = [1,2,3,4,5] println 3 in array prints true. What do I need to overload to support in for any object? Example: class Whatever { def addItem(item) { // add the item } } def w = new Whatever() w.addItem("one") w.addItem("two") println "two" in w I know I could make the collection this class uses public, but I'...

What kinds of operator overloads does Delphi support?

I wonder what sort of operator overloads are possible (and with what version of Delphi)? Thanks to Hallvard's great write-up on operator overloading, I know of : Add(Left, Right: Type): Type (for the '+' operator) Subtract(Left, Right: Type): Type (for the '-' operator) Multiply(Left, Right: Type): Type (for the '*' operator) Divide(L...

C++ operator overloading and implicit conversion

Hi, everyone! I have a class that encapsulates some arithmetic, let's say fixed point calculations. I like the idea of overloading arithmetic operators, so I write the following: class CFixed { CFixed( int ); CFixed( float ); }; CFixed operator* ( const CFixed& a, const CFixed& b ) { ... } It all works. I can write 3 * CFixe...

C++ user-defined conversion operators without classes?

In C++ is it possible to define conversion operators which are not class members? I know how to do that for regular operators (such as +), but not for conversion operators. Here is my use case: I work with a C Library which hands me out a PA_Unichar *, where the library defines PA_Unichar to be a 16-bit int. It is actually a string code...

Overload bracket access and assignment C++

I'm writing a hash table for my data structs class, and I'd like to add a little syntactic sugar to my implementation. template <typename HashedObj, typename Object> Object & Dictionary<HashedObj, Object>::operator[](HashedObj & key) { return items.lookup(key); } That works fine when I do something like cout << dict["mykey"]. But h...