operator-overloading

Unary Operator-() on zero values - c++

此生再无相见时 提交于 2019-12-13 13:27:15
问题 I wrote this code to overload the unary operator- on a matrix class: const RegMatrix RegMatrix::operator-()const{ RegMatrix result(numRow,numCol); int i,j; for(i=0;i<numRow;++i) for(j=0;j<numCol;++j){ result.setElement(i,j,(-_matrix[i][j])); } return result; } When i ran my program with debugger in visual studio, it showed me that when the operation is done on a double equals zero, it inserts the result matrix the number -0.00000. Is it some weird VS-display feature, or is it something i

overloaded 'operator+' must be a unary or binary operator error

空扰寡人 提交于 2019-12-13 12:56:39
问题 Following the advice given in this answer, I have overloaded the + operator in my simple Point class as follows (the += overload works fine). Point operator+ (Point p1, const Point& p2) { return std::move(p1 += p2); } But I get an error saying overloaded 'operator+' must be a unary or binary operator (has 3 parameters) What is wrong? 回答1: It sounds like you have declared your operator as a member function. A member function takes an implicit first parameter, meaning your operator now takes

Creating a class indexer operator[] allowing string parameter (string index)

寵の児 提交于 2019-12-13 12:27:26
问题 I want to create a class in c++. This class must manage with a collection. OK, no problem, I would like to use operator[] of course but, in this case, my wish is to index not by position, but by name ==> that means using a string indexer. It seems that something of this kind is not so nice to my compiler: // In hpp class myclass { ... ... std::string operator[](const std::string& name); } // In cpp std::string myclass::operator[](const std::string& name) { ... } // In main myclass m; std:

Special behavior for decltype of call operator for incomplete types

白昼怎懂夜的黑 提交于 2019-12-13 11:47:38
问题 I've been struggling with a compilation issue, and have been able to shrink the problem down to a small code segment. To set the stage, I'm trying to do CRTP, where the base method calls another in the derived class. The complication is, I want to use trailing return types to get the type of forwarding directly to the the Derived class's method. This always fails to compile unless I forward to the call operator in the derived class. This compiles: #include <utility> struct Incomplete;

Is this use of the “,” operator considered bad form?

非 Y 不嫁゛ 提交于 2019-12-13 11:37:40
问题 I have made a list class as a means of replacing variadic functions in my program used for initializing objects that need to contain a changing list of elements. The list class has a usage syntax that I really like. However I haven't seen it used before, so I was wondering if I shouldn't use it just because of that fact? A basic implementation of the list class looks like this... #include <list> #include <iostream> template<typename T> struct list { std::list<T> items; list(const list&ref)

==operator overload only on certain occasions

末鹿安然 提交于 2019-12-13 11:05:03
问题 I am making a project that takes in types as templates. The operator== is already overloaded for chars, ints, strings, etc as you know, but if the user decides to pass in a cstring (null terminated character array) I will need to overload the == for that. Can I choose to only overload the operator== when the user uses cstrings, and use the default == when they dont? How would this be accomplished? 回答1: You can't overload operator== for C strings, because they are pointers and operators can be

Completely custom stream operator in c++

北战南征 提交于 2019-12-13 10:51:57
问题 I want to create a completely custom stream pipeline for my video stabilization project in c++. The endresult should look like: videofilelocation >> preprocessing() >> analyze() >> stabilize() >> video_out(outputfilename).flush(); Therefore, preprocessing should accept an input string and load the video, extract frames, etc. Afterward, it should return a custom struct framevector which should be passed to analyze and so on. Unfortunately, there is no clear tutorial/explanation on how to

No '==' operator found which takes a left-hand operand of const Type

断了今生、忘了曾经 提交于 2019-12-13 09:49:14
问题 I was refactoring some of my code to use a structure instead of a single object. As a result of this change I needed to add in the operator for '==' so my existing vector involving functions could correctly evaluate my objects stored in the vectors. class IdentifyingClass; //forward declare class Class ReturnData { public: IdentifyingClass* value; float some_trivial_additional_data; bool operator==(const ReturnData& query_obj) { return value == query_obj.value; } bool operator==(const

Cython: counter-tutorial behavior

别来无恙 提交于 2019-12-13 07:05:11
问题 Tutorial says .pyx and .pxd files should not have the same name unless .pyx is the realization of the definitions from .pxd file. Note that the name of the .pyx file must be different from the cqueue.pxd file with declarations from the C library, as both do not describe the same code. A .pxd file next to a .pyx file with the same name defines exported declarations for code in the .pyx file. As the cqueue.pxd file contains declarations of a regular C library, there must not be a .pyx file with

A generic print function

谁都会走 提交于 2019-12-13 06:49:59
问题 I don't have a good C++ book on hand and google pulls ups nothing useful. How does this work? Conceptually what is going on here? Technically, is the prototype for operator<<() predefined, how did the writer of this know how to write it so that << is overloaded to output Container values? Where can I go to look at the operator<<() so that I can overload it? Also for an input you need a start and an end "place" c.begin() , c.end() ...but for output you need one "place" ostream_iterator . This