コンパイラ エラー C2668

'function' : オーバーロードされた関数のあいまいな呼び出し

指定されたオーバーロードされた関数呼び出しを解決できませんでした。 1 つ以上の実際のパラメーターを明示的にキャストすることをお勧めします。

このエラーは、テンプレートを使用して取得することもできます。 同じクラスに通常のメンバー関数と、同じシグネチャを持つテンプレート化されたメンバー関数がある場合は、テンプレート化されたメンバー関数が先である必要があります。 この制限は、Visual C++ の現在の実装に残っています。

次の例では C2668 エラーが生成されます。

// C2668.cpp
struct A {};
struct B : A {};
struct X {};
struct D : B, X {};

void func( X, X ){}
void func( A, B ){}
D d;
int main() {
   func( d, d );   // C2668 D has an A, B, and X
   func( (X)d, (X)d );   // OK, uses func( X, X )
}

このエラーを解決するもう 1 つの方法は、using 宣言を使用する方法です。

// C2668b.cpp
// compile with: /EHsc /c
// C2668 expected
#include <iostream>
class TypeA {
public:
   TypeA(int value) {}
};

class TypeB {
   TypeB(int intValue);
   TypeB(double dbValue);
};

class TestCase {
public:
   void AssertEqual(long expected, long actual, std::string
                    conditionExpression = "");
};

class AppTestCase : public TestCase {
public:
   // Uncomment the following line to resolve.
   // using TestCase::AssertEqual;
   void AssertEqual(const TypeA expected, const TypeA actual,
                    std::string conditionExpression = "");
   void AssertEqual(const TypeB expected, const TypeB actual,
                    std::string conditionExpression = "");
};

class MyTestCase : public AppTestCase {
   void TestSomething() {
      int actual = 0;
      AssertEqual(0, actual, "Value");
   }
};

intlong および void* の両方への変換が必要なので、定数 0 を使用したキャストでの変換はあいまいです。 このエラーを解決するには、使用されている関数パラメーターの正確な型に 0 をキャストします。 そうすると、変換を行う必要はありません。

// C2668c.cpp
#include "stdio.h"
void f(long) {
   printf_s("in f(long)\n");
}
void f(void*) {
   printf_s("in f(void*)\n");
}
int main() {
   f((int)0);   // C2668

   // OK
   f((long)0);
   f((void*)0);
}

このエラーは、CRT にすべての算術関数の floatdouble 形式が含まれると発生する可能性があります。

// C2668d.cpp
#include <math.h>
int main() {
   int i = 0;
   float f;
   f = cos(i);   // C2668
   f = cos((float)i);   // OK
}

このエラーは、CRT で math.h から pow(int, int) が削除されると発生する可能性があります。

// C2668e.cpp
#include <math.h>
int main() {
   pow(9,9);   // C2668
   pow((double)9,9);   // OK
}

このコードは Visual Studio 2015 では成功しますが、Visual Studio 2017 以降では C2668 で失敗します。 Visual Studio 2015 では、コンパイラは通常の copy-initialization と同じ方法で copy-list-initialization を誤って処理していました。 オーバーロードの解決のためにコンストラクターの変換のみを考慮していました。

struct A {
    explicit A(int) {}
};

struct B {
    B(int) {}
};

void f(const A&) {}
void f(const B&) {}

int main()
{
    f({ 1 }); // error C2668: 'f': ambiguous call to overloaded function
}