operator-overloading

Why does my operator overloading handle left versus right objects?

旧巷老猫 提交于 2019-12-12 00:51:03
问题 I had a question on operator overloading. Below is my the example code. If you can read through it and my question is below. //class definition class example { private: int a ; // Defined in FeetInches.cpp public: void seta(int f) { a = f; } example operator + (const example &); // Overloaded + int geta() { return a; } }; example example::operator + (const example &right) { example temp; temp.a = a + right.a; return temp; } //main #include "header" //this is the class definition above

Custom ordered set give “invalid operator<” of find(…)

好久不见. 提交于 2019-12-11 23:53:30
问题 Have a issue when I'm trying to find element in a custom ordered set. File: c:\program files (x86)\microsoft visual studio 10.0\vc\include\xtree Line: 1746 Expression: invalid operator< I need a set of strings where the elements are ordered accordingly my needs. Comparator object: struct OrderComparator { public: static map<string,int> valueOrder; bool operator()( const string lhs, const string rhs ) { map<string,int>::iterator resultLhs,resultRhs; resultLhs = valueOrder.find(lhs); resultRhs

C++ error constructor parameter

守給你的承諾、 提交于 2019-12-11 23:27:08
问题 i'm playing around with inheritance. I'm completely baffled by the compiler error i'm getting and looking it up it seems completely unrelated to what i'm trying to do which is simply initialize my class. here are the errors: In file included from main.cpp:2:0: student.h: In constructor ‘Student::Student(std::string, int, std::string, double)’: student.h:13:3: error: class ‘Student’ does not have any field named ‘gnu_dev_major’ student.h: In function ‘std::ostream& operator<<(std::ostream&,

Difference between a+b and operator+(a,b)

家住魔仙堡 提交于 2019-12-11 19:48:11
问题 Consider the following program: #include<functional> typedef std::function< int( int ) > F; F operator+( F, F ) { return F(); } int f( int x ) { return x; } int main() { operator+(f,f); // ok f+f; // error: invalid operands to binary expression } Why does the last line f+f; not compile? Why is it not identical to operator+(f,f); ? A reference to the standard would be appreciated. 回答1: The type of f is a built-in type. Operations on objects of built-in types never consider user-define

How can I overload operator= for lambda assignments?

落花浮王杯 提交于 2019-12-11 19:38:55
问题 I am trying to insert a function into a map but I want to check it first so I would like to overload assignment operation for std::function, is this possible? I try to overload the assignment operation, so if something other than expected is assigned, the assignment operator function should wrap it in the expected function and return it. #include <iostream> #include <map> #include <functional> class MyClass{ public: std::map<int, std::map<int, std::function<void(int,int)>>> events; std:

overloading operator * c++

╄→尐↘猪︶ㄣ 提交于 2019-12-11 19:25:27
问题 I have been trying to compile this program but it is giving me an error in regards to overloading the * operator for one of the functions: complex operator *(double n)const When I try to compile I get the error: no match for 'operator*' in '2 * c' Here is the header file: Complex.h #ifndef COMPLEX0_H #define COMPLEX0_H class complex { double realNum; double imagNum; public: complex(); complex(double x,double y); complex operator *(double n)const; complex operator *(const complex &c1)const;

overloading operators, what is the purpose of the overladed operator in this example

南楼画角 提交于 2019-12-11 18:40:06
问题 I have been trying to understand the use of the overloaded operator in this code, however, I cannot wrap my mind around it. I do not understand exactly the purpose of the overloaded operator or why it is needed. I know that this is a newbie question but if someone could explain it I would greatly appreciate it. const int Card::operator+(const Card& B) { if ((faceValue ==1 || B.faceValue == 1) && (faceValue + B.faceValue)<= 11) return 10 + faceValue + (B.faceValue); else return faceValue + (B

Overloading the comparison operators == C++

笑着哭i 提交于 2019-12-11 18:31:19
问题 I have a base class Person with 3 instance vars. Person(string name, unsigned long id, string email) and one derived class Student that inherits Person and have one new instance var year Student(string name, unsigned long id,int year,string email): Person(name,id,email) and one class Teacher that isn't necessary to describe. Then a have the class named eClass and I want Overload the comparison operator == and use that operator in function bool exists() when I compile my .cpp i have that error

program not calling the right overloaded operator

不羁的心 提交于 2019-12-11 18:22:23
问题 I've the class Directory which does stuff with objects of class File, and overloaded operators, like this: class Directory { std::vector<char> name, timeOfCreation; std::vector<File> arr; public: Directory(std::string); Directory(const Directory&); ~Directory(); Directory operator += (File); Directory operator += (Directory); Directory operator -= (File); Directory operator ~(); File operator [] (int); operator int(); friend bool operator % (Directory, File); friend bool operator % (Directory

no match for operator >> in std::cin

久未见 提交于 2019-12-11 18:14:19
问题 I have a class employee #include <iostream> #include <string> #include <fstream> using namespace std; class employee { public: double operator + (employee); istream& operator>> (istream&); employee(int); double getSalary(); private: double salary; }; int main() { employee A(400); employee B(800); employee C(220); cin>>C; } employee::employee(int salary) { this->salary = salary; } double employee::operator + (employee e) { double total; total = e.salary + this->salary; return total; } double