Condividi tramite


Errore del compilatore C2662

'function': impossibile convertire il puntatore 'this' da 'type1' a 'type2'

Osservazioni:

Il compilatore non è riuscito a convertire il this puntatore da type1 a type2.

Questo errore può essere causato dal richiamo di una funzione nonconst membro in un const oggetto . Possibili risoluzioni:

  • Rimuovere l'oggetto dalla dichiarazione dell'oggetto const .

  • Aggiungere const alla funzione membro.

Esempi

L'esempio seguente genera l'errore C2662:

// C2662.cpp
class C {
public:
   void func1();
   void func2() const{}
} const c;

int main() {
   c.func1();   // C2662
   c.func2();   // OK
}

Durante la compilazione con /clr, non è possibile chiamare una funzione su un const tipo gestito qualificato o volatile . Non è possibile dichiarare una funzione membro const di una classe gestita, pertanto non è possibile chiamare metodi su oggetti gestiti const.

// C2662_b.cpp
// compile with: /c /clr
ref struct M {
   property M^ Type {
      M^ get() { return this; }
   }

   void operator=(const M %m) {
      M ^ prop = m.Type;   // C2662
   }
};

ref struct N {
   property N^ Type {
      N^ get() { return this; }
   }

   void operator=(N % n) {
      N ^ prop = n.Type;   // OK
   }
};

L'esempio seguente genera l'errore C2662:

// C2662_c.cpp
// compile with: /c
// C2662 expected
typedef int ISXVD;
typedef unsigned char BYTE;

class LXBASE {
protected:
    BYTE *m_rgb;
};

class LXISXVD:LXBASE {
public:
   // Delete the following line to resolve.
   ISXVD *PMin() { return (ISXVD *)m_rgb; }

   ISXVD *PMin2() const { return (ISXVD *)m_rgb; };   // OK
};

void F(const LXISXVD *plxisxvd, int iDim) {
   ISXVD isxvd;
   // Delete the following line to resolve.
   isxvd = plxisxvd->PMin()[iDim];

   isxvd = plxisxvd->PMin2()[iDim];
}