編譯器錯誤 C2668

'function' : 多載函式的模棱兩可呼叫

無法解析指定的多載函式呼叫。 您可能想要明確轉換一或多個實際參數。

您也可以透過範本使用來取得此錯誤。 如果在同一個類別中,您有一般成員函式和具有相同簽章的樣板化成員函式,則範本化成員函式必須優先使用。 這項限制仍保留在 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 )
}

另一個解決此錯誤的方法是宣告 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");
   }
};

使用常數 0 轉換的轉換模棱兩可,因為 int 需要將 和 轉換成 longvoid* 。 若要解決此錯誤,請將 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 現在具有 float 所有數學函式的 和 double 形式。

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

此錯誤可能發生,因為 pow(int, int) 已從 math.h CRT 中移除 。

// 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-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
}