标签: overloading

c#:Func <T>参数中的重载决策

写这个功能:

static TResult reduce<TSource, TResult>(ParallelQuery<TSource> source,
                                        Func<TResult> seedFactory,
                                        Func<TResult, TSource, TResult> aggregator) {
    return source.Aggregate(seedFactory, aggregator, aggregator, x => x);
}                
Run Code Online (Sandbox Code Playgroud)

但我得到一个编译错误:

误差为1方法的类型参数"System.Linq.ParallelEnumerable.Aggregate( ,System.Linq.ParallelQuery<TSource>, TAccumulate,, System.Func<TAccumulate,TSource,TAccumulate> )"不能从使用推断.尝试显式指定类型参数.System.Func<TAccumulate,TAccumulate,TAccumulate>System.Func<TAccumulate,TResult>

我想要使​​用的重载是这一个, 而编译器似乎认为它也可以是这个.

我该怎么帮忙呢?

c# generics overloading overload-resolution

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

Go是否支持内置类型(例如map和slice)的运算符重载?

在python中,我可以通过定义来定义覆盖列表项访问和dict值访问的类型__getitem__()。我可以在Go中做类似的事情吗?

// What I mean is:
type MySlice []MyItem
// Definition of MySlice
......
func (s MySlice) getItem(i int) MyItem {
}
......
// Access is overrided with calling getItem()
item := ms[0] //calling ms.getItem(0)
// Is this doable?
Run Code Online (Sandbox Code Playgroud)

overloading built-in go

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

Java方法重载:参数的顺序是否会影响延迟?

假设我使用方法X跟随A类.我可以通过以下两种方式重载方法.

A{
  void X(foo p, bar q);
  void X(foo p, baq q);
}

A{
  void X(foo p, bar q);
  void X(baq q, foo p);
}
Run Code Online (Sandbox Code Playgroud)

第二个实现是否应该首选,因为它需要更少的时间来解决并找到正确的功能?

如果需要进一步澄清,请与我们联系.

java optimization overloading

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

通过向 VB.NET 函数添加可选参数可能会破坏现有代码吗?

如果我向到处使用的函数添加新的可选参数,大型项目中的现有代码是否有可能崩溃?我知道我可以重载该函数并将风险降到最低,但实际上......如果我坚持使用可选参数,会有什么风险?

这是一个例子:

    Public Function GetContent(ByVal URL As String, ByVal ID As String, Optional ByRef PageTitle As String = "") As String
        Try
            Dim web As New HtmlWeb()
            Dim doc As HtmlDocument = web.Load(URL)
            ID = "//div[@id='" & ID & "']"
            Dim ContentNode As HtmlNode = doc.DocumentNode.SelectSingleNode(ID)

            ' The two lines below are the mere extent of what's new inside this function, besides the new Optional ByRef parameter in its signature
            Dim PageTitleNode As HtmlNode = doc.DocumentNode.SelectSingleNode("//title")
            If Not PageTitleNode Is …
Run Code Online (Sandbox Code Playgroud)

com clr overloading signature optional-parameters

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

重载std :: exception - what()无法转换const char*

我正在编写我的异常类:

class MyExcept: public std::exception
{
public:
    MyExcept(std::string _msg);
    virtual ~MyExcept() throw();
    virtual const char* what() const throw();

private:
    std::string m_errorMsg;
};


MyExcept::MyExcept(std::string _msg)
: m_errorMsg(_msg)
{
}

MyExcept::~MyExcept() throw()
{
}

const char* MyExcept:: what() const throw()
{
    return m_errorMsg.c_str;
}
Run Code Online (Sandbox Code Playgroud)

我用g ++编译并在函数what()中返回以下错误:

不能将'std :: __ cxx11 :: basic_string <_CharT,_Traits,_Alloc> :: c_str,std :: allocator>'从类型'const char*(std :: __ cxx11 :: basic_string ::)()const'转换为类型'const char*'返回m_errorMsg.c_str;

我做错了什么?谢谢

c++ overloading exception

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

重载函数的调用不明确

我收到错误调用重载函数不明确,我明白这是因为编译器无法区分它们,但是有没有办法在保持相同参数的同时解决这个问题?我必须使用下面提供的声明,如果每次都遇到此错误,我会很困惑如何使用它们。

我已经缩短了代码以显示提出问题的构造函数。

错误消息.h

class ErrorMessage {
        char* message_; //pointer that holds the address of the message stored in current object
    public:
        ErrorMessage();
        explicit ErrorMessage(const char* errorMessage = nullptr); //receive address of a C-style nullterminate string holding an error message
}
Run Code Online (Sandbox Code Playgroud)

错误消息.cpp

命名空间 sict {

ErrorMessage::ErrorMessage() {
    message_ = nullptr;
}

ErrorMessage::ErrorMessage(const char* errorMessage) {
    if(errorMessage == nullptr) {
        message_ = nullptr;
    }
    else {
        message(errorMessage);
    }
    const char* ErrorMessage::message() const {
        return message_;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ overloading

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

是否有更简洁的方法用不同的参数编写相同的方法?

我需要有多种方法采用不同的参数.是否有更简洁的方法来编写这些方法而不是分别声明它们中的每一个?我总共需要4个相同的方法.我能写一个,但让它决定传递什么参数?或者我必须最终复制并粘贴第一个3次并更改参数.这是其中两个

public String findLogNumber(XWPFWordExtractor we) {
    int logIndex;
    int logIndexEnd;
    String logNumber = "";
    if (we.getText().contains("Log ")) {
        logIndex = we.getText().indexOf("Log ") + 4;
        logIndexEnd = logIndex + 5;
        logNumber = we.getText().substring(logIndex, logIndexEnd);
    }
    return logNumber;
}

public String findLogNumber(WordExtractor we) {
    int logIndex;
    int logIndexEnd;
    String logNumber = "";
    if (we.getText().contains("Log ")) {
        logIndex = we.getText().indexOf("Log ") + 4;
        logIndexEnd = logIndex + 5;
        logNumber = we.getText().substring(logIndex, logIndexEnd);
    }
    return logNumber;
}
Run Code Online (Sandbox Code Playgroud)

java methods overloading

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

C++:向量<>的运算符[]重载

我正在尝试[]在向量上重载运算符.我试图让这个运算符在Matlab或Python中工作,用于负索引或大于矢量长度的索引.我的问题不是得到正确的结果,而是实际重载运算符,知道我将在重载代码中使用非重载运算符.我需要它用于特定类的向量,但如果它适用于任何类型它会更好vector.

现在我的代码是:header:

MyClass std::vector::operator[](std::vector<MyClass> const vector,int const idx) const;
Run Code Online (Sandbox Code Playgroud)

资源:

Myclass vector::operator[](vector<MyClass> const vector,int const idx) const {
    n=vector.size()
    if((idx>=0) && (idx<n)){
        return vector[idx];
    }
    else if(idx<0){
        return vector[n+idx%n]
    }
    else{
        return vector[idx%n]
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到错误:

error: 'template<class _Tp, class _Alloc> class std::vector' used without template parameters
error: non-member function 'MyClass operator[](std::vector<MyClass>, int)' cannot have cv-qualifier
error: 'MyClass operator[](std::vector<MyClass>, int)' must be a nonstatic member function
Run Code Online (Sandbox Code Playgroud)

如果这个问题已经讨论过了,我很抱歉,但是我找不到它......非常感谢你提前得到答案!

c++ overloading operator-overloading operators stdvector

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

为什么没有任何东西传递给我的重载函数?

第一张海报在这里。首先,我使用 .NET 4.8 和 C#。我在重载函数方面遇到了一些问题,该函数似乎没有获得任何参数输入。基本上我的函数试图做的是获取单个对象或对象列表(所有自定义类“卡片”)并确定其中一个是否与调用该函数的对象具有相同的派系。但是,当我调试程序时,第一个函数完美地运行并符合预期,但第二个函数似乎根本没有得到任何输入;它得到一个长度为 0 的对象。'Factions' 是一个命名空间范围的公共枚举;我不认为那里有任何问题。

类和函数定义:

public class Card
{
    /* Constructors, variables, and other functions left out for simplicity */

    public bool SameFaction(Card card)
    {
        if (card.Faction.Equals(this.Faction))
            return true;
        return false;
    }

    public bool SameFaction(List<Card> hand)
    {
        foreach (Card card in hand)
        {
            if (card.Faction.Equals(this.Faction))
            {
                return true;
            }
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

执行:

Card card1 = new Card(Factions.faction1);
List<Card> listOfCards = new List<Card>();
{
    new Card(Factions.faction1);
    new Card(Factions.faction2);
    new Card(Factions.faction3);
    new Card(Factions.faction4);
};

card1.SameFaction(new …
Run Code Online (Sandbox Code Playgroud)

.net c# overloading function

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

如果函数参数类型相同,为什么在 Java 中强制转换函数调用的两个参数是多余的?

我很想知道转换在函数调用中是如何工作的。我的类中有两个特定函数的重载,称为execute. 第一个重载是基本实现并接受double类型参数。第二个重载接受int类型参数。第二个重载也调用基本重载,并通过将参数强制转换为double

基本实现如下所示:

public double execute(double leftVal, double rightVal) throws IOException {
    ...
    return solution;
}
Run Code Online (Sandbox Code Playgroud)

重载版本如下所示:

public int execute(int leftVal, int rightVal) throws IOException {
    return (int) execute((double) leftVal, (double) rightVal);
}
Run Code Online (Sandbox Code Playgroud)

为什么上面的(double) leftVal, (double) rightVal部分,特别是部分,是多余的,为什么它可以在删除一个演员表的情况下工作?在第一个重载的调用工作无论何种顺序铸造中,两者execute(leftVal, (double)rightVal)execute((double) leftVal, rightVal)正常执行,不产生错误。我一直认为 Java 在明确识别类型方面非常严格。我希望有一些警告或错误,即不存在接受调用之类的函数execute(double, int)。不过,我有一个想法,即第一个强制转换有助于编译器确定选择哪个重载,因此第二个强制转换是隐式的。也许是因为这些类型是原始的,易于强制转换?还有一个小问题,两个函数都称为重载还是第一个定义之后的每个定义都是原始函数的重载?

java redundancy overloading

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