Delen via


Compilerfout C2668

'functie' : dubbelzinnige aanroep naar overbelaste functie

Opmerkingen

De opgegeven overbelaste functie-aanroep kan niet worden opgelost. Mogelijk wilt u een of meer van de werkelijke parameters expliciet casten.

U kunt deze fout ook krijgen via sjabloongebruik. Als u in dezelfde klasse een gewone lidfunctie en een sjabloonlidfunctie met dezelfde handtekening hebt, moet de sjabloon eerst komen. Deze beperking blijft aanwezig in de huidige implementatie van Visual C++.

Voorbeelden

In het volgende voorbeeld wordt C2668 gegenereerd:

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

Een andere manier om deze fout op te lossen is met een using declaratie:

// 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");
   }
};

Conversie op een cast met constante 0 is dubbelzinnig omdat int een conversie zowel naar long als naar void*vereist. Als u deze fout wilt oplossen, castt u 0 naar het exacte type van de functieparameter waarvoor deze wordt gebruikt. Dan hoeven er geen conversies plaats te vinden.

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

Deze fout kan optreden omdat de CRT nu float en double vormen van alle wiskundige functies heeft.

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

Deze fout kan optreden omdat de pow(int, int) is verwijderd uit math.h de CRT.

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

Deze code slaagt in Visual Studio 2015, maar mislukt in Visual Studio 2017 en hoger met C2668. In Visual Studio 2015 heeft de compiler ten onrechte de copy-list-initialisatie op dezelfde manier behandeld als normale copy-initialisatie. Het werd beschouwd als alleen het converteren van constructors voor overbelastingsresolutie.

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
}