operator-overloading

C++ can't find function out of namespace

假装没事ソ 提交于 2021-02-19 02:56:08
问题 Compiling the following code fails because the second function can't find the first one, even though it's outside namespaces. I couldn't figure out the problem myself, and so far I haven't found any answers on the net. test.cpp: #include <bits/stdc++.h> struct myclass {}; template <typename T, typename U> std::ostream& operator<< (std::ostream &os, const std::pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } namespace my { void operator<< (std::ostream os, myclass

Preventing implicit conversion operator only for binary operators

走远了吗. 提交于 2021-02-19 02:22:09
问题 I'm having an issue that I've boiled down to the following, where an == operator usage compiles even though it's supposed to fail (C++17, tested on GCC 5.x, 8.x, and 9.x): template <int N> struct thing { operator const char * () const { return nullptr; } bool operator == (const thing<N> &) const { return false; } }; int main () { thing<0> a; thing<1> b; a == b; // i don't want this to compile, but it does } The reason it is compiling is because the compiler is choosing to do this: (const char

C++, overload * for matrix multiplication

南笙酒味 提交于 2021-02-11 08:50:24
问题 I'm having a great deal of trouble trying to overload the multiplication operator * for matrix multiplication. I've defined a matrix class #ifndef MMATRIX_H #define MMATRIX_H #include <vector> #include <cmath> // Class that represents a mathematical matrix class MMatrix { public: // constructors MMatrix() : nRows(0), nCols(0) {} MMatrix(int n, int m, double x = 0) : nRows(n), nCols(m), A(n * m, x) {} // set all matrix entries equal to a double MMatrix &operator=(double x) { for (int i = 0; i

Java, operator overloading and “+” operator for String

喜夏-厌秋 提交于 2021-02-10 20:18:30
问题 I have a doubt regarding operator overloading in Java. I know that Java does not support operator overloading but then what is the "+" operator doing in below valid Java program: import java.util.*; import java.lang.*; import java.io.*; class OperatorOverloadingTest { public static void main (String[] args) throws java.lang.Exception { String str1 = "Operator "; String str2 = "overloading"; String str3 = str1+str2; System.out.println(str3); } } Stdout: Operator overloading 回答1: This operator

Error C2676: std::set::const_iterator doesn't have operator+ function?

断了今生、忘了曾经 提交于 2021-02-10 20:09:42
问题 std::set<int> s = { 1,2,3,4,5 }; std::set<int> s2(s.begin(), s.begin() + 2); I want to assgin several values of s into s2 . But got below compile error: Error C2676 binary ' + ': ' std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> ' does not define this operator or a conversion to a type acceptable to the predefined It seems std::set::const_iterator doesn't have operator+ method. 回答1: It seems std::set::const_iterator doesn't have operator+ method. You are right about

Error C2676: std::set::const_iterator doesn't have operator+ function?

巧了我就是萌 提交于 2021-02-10 20:08:38
问题 std::set<int> s = { 1,2,3,4,5 }; std::set<int> s2(s.begin(), s.begin() + 2); I want to assgin several values of s into s2 . But got below compile error: Error C2676 binary ' + ': ' std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> ' does not define this operator or a conversion to a type acceptable to the predefined It seems std::set::const_iterator doesn't have operator+ method. 回答1: It seems std::set::const_iterator doesn't have operator+ method. You are right about

How do I specify the expected result of a `std::ops::Mul` in a trait bound?

守給你的承諾、 提交于 2021-02-10 18:01:35
问题 I have: use std::ops::{Add, Div, Mul, Neg, Sub}; pub trait Hilbert: Add + Sub + Mul + Div + Neg + Mul<f64> + Div<f64> + Sized { fn dot(&self, other: &Self) -> f64; fn magnitude(&self) -> f64; } fn g<T: Hilbert>(x: T) -> f64 { return (x * 2.0).dot(x); } ...which yields: error[E0599]: no method named `dot` found for type `<T as std::ops::Mul<f64>>::Output` in the current scope --> src/main.rs:9:22 | 9 | return (x * 2.0).dot(x); | ^^^ | = help: items from traits can only be used if the trait is

What is the correct way to define an already existing (e.g. in Prelude) operator between a user-defined type and an existing type?

我与影子孤独终老i 提交于 2021-02-09 09:22:49
问题 Suppose I have a custom type wrapping an existing type, newtype T = T Int deriving Show and suppose I want to be able to add up T s, and that adding them up should result in adding the wrapped values up; I would do this via instance Num T where (T t1) + (T t2) = T (t1 + t2) -- all other Num's methods = undefined I think we are good so far. Please, tell me if there are major concerns up to this point. Now let's suppose that I want to be able to multiply a T by an Int and that the result should

What is the correct way to define an already existing (e.g. in Prelude) operator between a user-defined type and an existing type?

我只是一个虾纸丫 提交于 2021-02-09 09:20:23
问题 Suppose I have a custom type wrapping an existing type, newtype T = T Int deriving Show and suppose I want to be able to add up T s, and that adding them up should result in adding the wrapped values up; I would do this via instance Num T where (T t1) + (T t2) = T (t1 + t2) -- all other Num's methods = undefined I think we are good so far. Please, tell me if there are major concerns up to this point. Now let's suppose that I want to be able to multiply a T by an Int and that the result should

What is the correct way to define an already existing (e.g. in Prelude) operator between a user-defined type and an existing type?

你。 提交于 2021-02-09 09:16:15
问题 Suppose I have a custom type wrapping an existing type, newtype T = T Int deriving Show and suppose I want to be able to add up T s, and that adding them up should result in adding the wrapped values up; I would do this via instance Num T where (T t1) + (T t2) = T (t1 + t2) -- all other Num's methods = undefined I think we are good so far. Please, tell me if there are major concerns up to this point. Now let's suppose that I want to be able to multiply a T by an Int and that the result should