标签: overloading

具有重载 less 运算符的 std::max 无法编译

这是代码:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <utility>

struct student
{
    std::string full_name;
    int group;
    int number;

    friend std::istream& operator>>(std::istream& in, student& obj)
    {
        in >> obj.full_name >> obj.group >> obj.number;
        return in;
    }
    friend std::ostream& operator<<(std::ostream& out, const student& obj)
    {
        out << "Full name: " << obj.full_name << '\n';
        out << "Group: " << obj.group << '\n';
        out << "Number: " << obj.number << '\n';
        return out;
    }

    bool operator<(const student& obj)
    {
        return this->number …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-overloading std max

0
推荐指数
1
解决办法
206
查看次数

C++ 中是否有与 Python 的 __rmul__ 等效的函数?

Python 3 允许重载*.

例如,

a*b

允许a通过提供方法来定义结果__mul__

然而,如果该方法返回,则调用NotImplemented,__rmul__方法b(如果存在)以给出结果。此处记录了这一点。

这样做的一个优点是我可以在ba知道a.

C++ 中是否有等效的方法__rmul__,或者是否有实现相同或类似事物的方法?

c++ overloading operator-overloading

0
推荐指数
1
解决办法
179
查看次数

您能否避免数学运算符的多个运算符重载?

假设我有一个非常简单的 Rational 类,如下所示:

class Rational
{

    int numer;
    int denom;

public:

    Rational(const int& numer, const int& denom) : numer(numer), denom(denom) {}
    void operator*(const Rational& other) { std::cout << "multiply, simple as\n"; }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好。然后说我希望能够将 Rational 类与整数相乘,因此我向该类添加另一个函数:

class Rational
{
    
    int numer;
    int denom;
    
public:
    
    Rational(const int& numer, const int& denom) : numer(numer), denom(denom) {}
    void operator*(const Rational& other) { std::cout << "multiply, simple as\n"; }
    void operator*(const int& other) { std::cout << "some math here\n"; }
}
Run Code Online (Sandbox Code Playgroud)

好吧,没什么大不了的。除了我实际上无法执行以下操作,因为参数的顺序都是错误的:

Rational mine(1, …
Run Code Online (Sandbox Code Playgroud)

c++ overloading class operator-overloading

0
推荐指数
1
解决办法
101
查看次数

重载 &lt;&lt; 运算符时出现错误

我有一个名为“Vector”的类。它由两个私有字段组成:std::vector<double> coordinatesint len。方法dim()返回len

<<像这样重载运算符:

friend std::ostream& operator<<(std::ostream& os,  Vector& vec ) 
{
    std:: cout << "(";
    for ( int i = 0; i < vec.dim(); i++ ) {
        if ( i != vec.dim()-1){
            os << vec[i] << ", ";
        } else {
            os << vec[i];
        }
    }
    os << ')';
    return os;
}
Run Code Online (Sandbox Code Playgroud)

+像这样的运算符:

friend Vector operator +(Vector& first, Vector& second)
{
    if(first.dim() != second.dim()){
        throw std::length_error{"Vectors must be …
Run Code Online (Sandbox Code Playgroud)

c++ overloading vector operator-overloading

0
推荐指数
1
解决办法
96
查看次数

C++ 不能重载仅通过返回类型区分的函数

我是 C++ 新手,我正在编写一个程序来计算输入的年份是否为闰年,但这会返回一个错误,提示:无法重载仅按返回类型区分的函数。而且,不接受带有布尔表达式的运算符“==”。为什么?谢谢。这是我的代码:


using namespace std;

int leap_year(int year);


bool div(int num, int divid) {

    if (num % divid == 0)
        return true;
    else
        return false;
}

int leap_year(int year) {
    if (div(year, 400))
        return 365;
    else if (div(year, 100) & div(year, 400) == false)
        return 366;
    else
        return 365;
}

void main() {
    int year;
    cout << "Year: ";
    cin >> year;

    int result = leap_year(year);
    cout << result;
    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

c++ overloading leap-year

0
推荐指数
1
解决办法
446
查看次数

默认参数函数签名更改

我有一个函数,其当前签名是f(a,b = 0).我想添加另一个参数c.我希望我的函数能够以这种方式调用f(a,b),这是当前行为和f(a,c).一种方法是重载函数并复制功能代码.我不想从f(a,c)调用f(a,b).我在C++工作.

是否有任何标准设计模式或解决方案可以避免此代码重复?

c++ overloading

-1
推荐指数
1
解决办法
828
查看次数

如何使用不同的输入参数重载两个方法

我有这两个功能

private void calcResults()
{
   MakePath(id, results, _resultCount);
   MakePath(id, "XYZ", _resultSICount)
}

private string MakePath(string subFolder, object obj, int index)
{
    string dir = System.IO.Path.Combine(_outputDir, subFolder);
    string fileName = string.Format("{0} {1} {2}.xml",
           obj.GetType().Name, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString());
    return System.IO.Path.Combine(dir, fileName);
}

private string MakePath(string subFolder, string tempFileName, int index)
{
    string dir = System.IO.Path.Combine(_outputDir, subFolder);
    string fileName = string.Format("{0} {1} {2}.xml",
           tempFileName, _dateTimeSource.Now.ToString(DATE_FORMAT), index.ToString());
    return System.IO.Path.Combine(dir, fileName);
}
Run Code Online (Sandbox Code Playgroud)

请帮助一下.

谢谢

c# refactoring overriding overloading

-1
推荐指数
1
解决办法
6936
查看次数

C++重载运算符

我在我的程序中遇到了重载运算符"+"," - ","/"和"*"的问题.基本上我有一个对象,它是动态分配的double值数组.我有整个程序,但我无法克服那些重载.

我的构造函数看起来像这样:

table::table(int size) {
    this->size = size;
    tab = new double[size];
    count++;
}
Run Code Online (Sandbox Code Playgroud)

我写过类似的东西:

table & table::operator-(const table &tab3 )
{
        table * tab_oper2 = new table(size);
        for(int i=0; i< tab3.size; i++) 
        {
            (*this).tab[i] -= tab3.tab[i];
        }
        return *this;
}
Run Code Online (Sandbox Code Playgroud)

这通常是有效的,但通常不是这样做的好方法.我的导师告诉我尝试将(*this)切换到*tab_oper2,将其作为参考返回,但它不起作用.拜托,有人能告诉我怎么做得好吗?

c++ overloading operator-keyword

-1
推荐指数
1
解决办法
105
查看次数

-1
推荐指数
1
解决办法
72
查看次数

C++方法按继承参数类型重载

如果我有一个Base和一个派生类:

class Base {
  //...
};

class Derived : public Base {
  //...
};
Run Code Online (Sandbox Code Playgroud)

是否可以通过以下方式重载功能?

void DoSomething(Base b) {
    cout << "Do Something to Base" << endl;
}

void DoSomething(Derived d) {
    cout << "Do Something to Derived" << endl;
}
Run Code Online (Sandbox Code Playgroud)


如果我这样做会发生什么:

int main() {
    Derived d = Derived();
    DoSomething(d);
}
Run Code Online (Sandbox Code Playgroud)

Derived也是一个Base ..所以调用哪个版本?

c++ polymorphism inheritance overloading

-1
推荐指数
1
解决办法
1373
查看次数