标签: overloading

C++重载构造函数问题

我正在为我的C++类做一个家庭作业,但是我的重载构造函数很难.默认构造函数工作正常但重载构造函数不能正常工作.任何指导都会被批准.

    /*
Name:Isis Curiel
Date:04/05/2017
Assignment 10
Instructions:
Redo assignment 5 by

1. Defining a class Date that has

(1) Three member variables:

date,
month,
year
(2) member functions

constructor:  Initializes the Date to the given month, day and 
year or to the default value. you can overload the constructor,
or use default arguments.

example use:

Date today(3,31, 2017);  // 03/31/2017

Date firstDay;  // will be 01/01/2017

reset: reset the  month, day and year based on the parameters. So today.reset(4,1,2017); …
Run Code Online (Sandbox Code Playgroud)

c++ constructor overloading

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

Python3-在__eq__方法中使用super()会引发RuntimeError:super():__class__单元格未找到

我是猴子修补__eq__类的方法。我发现以下作品:

   def eq(obj, other):
       if isinstance(other, str):
          return obj.name.upper() == other.upper()
       else:
          return object.__eq__(obj, other)
Run Code Online (Sandbox Code Playgroud)

这不起作用:

  def eq(obj, other):
     if isinstance(other, str):
         return obj.name.upper() == other.upper()
     else:
        return super().__eq__(other)
Run Code Online (Sandbox Code Playgroud)

这有时可行,但有时会引发错误:

def eq(obj, other):
   if isinstance(other, str):
       return obj.name.upper() == other.upper()
   else:
       return super().__eq__(self, other)
Run Code Online (Sandbox Code Playgroud)

错误:

<ipython-input-128-91287536205d> in eq(obj, other)
      3         return obj.name.upper() == other.upper()
      4     else:
----> 5         return super().__eq__(self, other)
      6 
      7 

RuntimeError: super(): __class__ cell not found
Run Code Online (Sandbox Code Playgroud)

您能解释一下这里发生了什么吗?如何正确替换objectsuper()

python overloading monkeypatching super python-3.x

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

运算符重载意外结果

我不明白为什么一元运算符不起作用.我相信我错过了一些概念.请帮忙.

#include <iostream>
using namespace std;

class Distance {
   private:
      int feet;             // 0 to infinite
      int inches;           // 0 to 12
   public:
      // required constructors
      Distance(){
         feet = 0;
         inches = 0;
      }
      Distance(int f, int i){
         feet = f;
         inches = i;
      }
      // method to display distance
      void displayDistance() {
         cout << "F: " << feet << " I:" << inches <<endl;
      }
      // overloaded minus (-) operator
      Distance operator- () {
         return Distance(-feet, -inches);
      }
}; …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

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

C中函数重载的替代方法

我正在寻找一种优雅的方法来避免重写函数,其实现几乎相同,但只有签名(输入参数的数量和它们的数据类型)是不同的.我知道在C中不可能进行函数重载.我也知道可变函数的存在.但我认为他们在这种情况下不会有所帮助.

考虑以下问题,我们需要计算三角形的面积.我们有两个函数实现两个不同的公式:S = 1/2bh和S = sqrt(s(sa)(sb)(sc)).除了计算区域外,每个函数还修改参数nbnthr.最后,有一个顶级例程bisect_area_...,它在给定函数上启动二分程序area_tria1area_tria2为参数nb或函数优化它nthr.目前我明确地实现了两个二分函数:一个用于签名,area_tria1另一个用于签名area_tria2.我觉得必须有更好,更优雅的方式,才能拥有一个通用的二分函数bisect_area_tria().请注意,在实际情况中,我手边的输入参数数据类型也不同.

下面是函数签名的骨架伪代码:

// Calculate area of triangle, modify and return parameter 'nb'
void area_tria1_nb(..., int *nb, double b, double h, double *S) {

    // change parameter 'nb'
    ...

    S = 0.5*b*h;
}

// Calculate area of triangle, modify and return parameter 'nthr'
void area_tria1_nthr(..., int *nthr, double b, double h, double *S) {

    // change …
Run Code Online (Sandbox Code Playgroud)

c overloading variadic-functions

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

C++程序自动舍入除数组中第一个值之外的double值

我正在使用此代码进行函数重载,使用C++对double和整数数组进行排序.

 #include<iostream>
#include<bits/stdc++.h>
using namespace std;

void sort(int arr[],int n)
{
    int i,j,key;
    for(j=1;j<n;j++)
    {
        key=arr[j];
        i=j-1;
        while((i>=0)&&(arr[i]>key))
        {
            arr[i+1]=arr[i];
            i--;
        }
        arr[i+1]=key;
    }
    cout<<"Sorted integer array is \n";
    for(i=0;i<n;i++)
    {
        cout<<arr[i]<<endl;
    }
}

void sort(double arr[],int n)
{

    int i,j,key;
    for(j=1;j<n;j++)
    {
        key=arr[j];
        i=j-1;
        while((i>=0)&&(arr[i]>key))
        {
            arr[i+1]=arr[i];
            i--;
        }
        arr[i+1]=key;
    }
    cout<<"Sorted double array is \n";
    for(i=0;i<n;i++)
    {
        cout<<arr[i]<<endl;
    }
}

int main()
{
    int n;
    cout<<"Enter the size of the array \n";
    cin>>n;
    cout<<"Enter the values for the …
Run Code Online (Sandbox Code Playgroud)

c++ arrays double overloading function

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

接受元组并返回另一个元组的函数

如何编写一个函数重载,它接受任意大小的元组并返回另一个相同大小的元组,其中int变为double(添加0.5值),char变为string,size_t变为int(变为相反的符号),并且我们可能想要的任何其他类型的变化 例如,函数foo接受tuple<int, char, size_t>并返回tuple<double, string, int>,同时它接受tuple<size_t, char>并返回tuple<int, string>.

c++ overloading tuples function c++14

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

强制调用正确的方法重载

我有两种方法,具有以下签名:

public void DoSomething<T>(T value) { ... }

public void DoSomething<T>(IEnumerable<T> value) { ... }
Run Code Online (Sandbox Code Playgroud)

我试着像这样打电话给第二个:

DoSomething(new[] { "Pizza", "Chicken", "Cheese" });
Run Code Online (Sandbox Code Playgroud)

它仍然跳进了第一个.如何强制它将进入第二种方法呢?例如,通过对通用参数使用where子句?

编辑:

更确切地说:为什么它不起作用,即使我更具体并将重载更改为类似的东西,这将在尝试调用上面显示的方法时导致编译错误:

public void DoSomething<T>(T value) where T : IComparable { ... }

public void DoSomething<T>(IEnumerable<T> value) where T : IComparable { ... }
Run Code Online (Sandbox Code Playgroud)

.net c# generics overloading .net-core

0
推荐指数
2
解决办法
85
查看次数

C++ constexpr重载具有相同名称的构造函数

我确实理解constexpr在运行时可以计算的表达式上使用它时的用法.

我想为复数创建一个constexpr.x = 5_i应该创建一个复杂数量的我自己创建的复杂类,并且我需要一个constantexpr constructor.

class Complex {
private:
    double real_;
    double imag_;

public:
    ...
    Complex(double real, double imaginary);

    constexpr Complex(double real, double imaginary):
        real_(real),imag_(imaginary) {};

//Nonmember function
constexpr Complex operator""_i(long double arg);
Run Code Online (Sandbox Code Playgroud)

Complex(double real, double imaginary);稍后在.cpp文件中定义的.
当我尝试编译它时,我收到以下错误:

‘constexpr Complex::Complex(double, double)’ cannot be overloaded with  
‘Complex::Complex(double, double)’
Run Code Online (Sandbox Code Playgroud)

如果我只定义constexpr函数我的结论是我不能Complex::Complex(double, double)在运行时使用.

为什么我不能定义两个不同的功能?这在C++中是不允许的?编译器能否看到两个函数之间的区别?还有其他办法吗?

c++ overloading class constexpr

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

重载+ =用于链操作

比方说我有:

class Date {
    int year, month, day;
}
Run Code Online (Sandbox Code Playgroud)

我有+运算符重载:

friend CDate operator +(const Date &leftDate, const Date &rightDate) {}
Run Code Online (Sandbox Code Playgroud)

我在正确的日期增加左边的日期.那部分似乎有效.

现在我想超载+=,如果我所做的一切都是微不足道的date += another_date.

但是,如果我不得不链它,比如:date += another_date + another_date_2我要创建一个向量,another_dateanother_date2会被保存,然后做加法为他们每个人依次是:

Date& Date::operator +=(const vector<Date> &dates) {
    for(auto date: dates) {
        this->year += date.year;
        this->month += date.month;
        this->day += date.day;
    }
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

我现在正在努力的部分是如何重载+运算符,它返回一个向量?

我的想法到目前为止:

  • vector<Date> operator +(const Date &date):我在哪里创建一个向量?我必须创建一个才能插入date.
  • vector<Date> operator +(vector<Date> &dates, …

c++ overloading vector

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

传递NULL作为参数时,函数重载如何工作?

我知道NULL是#defined为0.它似乎是一个int转换为指针类型的常量.那么当有两个重载函数时会发生什么:一个采用指针类型,另一个采用int类型.这是如何工作的nullptr

#include <iostream>
using namespace std;

void callMe(int* p)
{
    cout << "Function 1 called" << endl;
}

void callMe(int i)
{
    cout << "Function 2 called" << endl;
}

int main()
{
    callMe(nullptr);
    callMe(NULL);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道callMe(nullptr)肯定会调用第一个函数.但是哪个函数会被调用callMe(NULL)?我在我的电脑上编译了这段代码.只有一个警告.

g++ -std=c++11 nullptr_type.cpp
nullptr_type.cpp: In function 'int main()':
nullptr_type.cpp:44:14: warning: passing NULL to non-pointer argument 1 of 'void callMe(int)' [-Wconversion-null]
   callMe(NULL);
          ^
Run Code Online (Sandbox Code Playgroud)

我的代码编译没有任何问题,当我运行它时,我看到callMe(NULL)称为函数2. Visual Studio也编译代码,它调用函数2.

但是,当我尝试在在线GeeksForGeeks IDE上编译我的代码时,我遇到了一些编译器错误.

https://ide.geeksforgeeks.org/UsLgXRgpAe

我想知道为什么会出现这些错误. …

c++ overloading overload-resolution c++11

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