Compilerfehler mit "template" unter Linux (g++)
-
Hi!
Wenn ich den Source unter Windows mit Visual Studio kompiliere, gibt es keinen Fehler. Wenn ich aber make/g++ unter Linux nehme, klappt das nich mit folgendem Fehler:
In file included from tDES.cpp:20:
tDES.h: In static member functionstatic void McbDESImpl<n>::McbCreateSubKeys(const unsigned char*, unsigned char (*)[6])': tDES.h:668: Fehler: Deklaration von »int n« tDES.h:637: Fehler: shadows template parmint n'
tDES.h: In static member functionstatic void McbDESImpl<n>::McbMapThroughSBox(const unsigned char*, const unsigned char*, unsigned char*)': tDES.h:919: Fehler: Deklaration von »int n« tDES.h:877: Fehler: shadows template parmint n'
tDES.h: In static member functionstatic void McbDESImpl<n>::McbEncode64Bits(const unsigned char*, unsigned char*, const unsigned char (*)[6], int)': tDES.h:1061: Fehler: Deklaration von »int n« tDES.h:1014: Fehler: shadows template parmint n'
tDES.h: In member functionbool McbDESImpl<n>::McbDoDES(const unsigned char*, long unsigned int, bool)': tDES.h:1203: Fehler: Deklaration von »long unsigned int n« tDES.h:1197: Fehler: shadows template parmint n'
tDES.cpp: In functionvoid McbTestSingleDES()': tDES.cpp:133: Warnung: int Format, long unsigned int Argument (Argument 2) tDES.cpp:53: Warnung: Variable »lpKey2« wird nicht verwendet tDES.cpp: In functionvoid McbTestTripleDES()':
tDES.cpp:199: Warnung: int Format, long unsigned int Argument (Argument 2)
tDES.cpp:202:24: Warnung: no newline at end of file
tDES.h: In constructorMcbDESImpl<n>::McbDESImpl() [with int n = 0]': tDES.cpp:75: instantiated from here tDES.h:492: Warnung:McbDESImpl<0>::m_nIterations' will be initialized after
tDES.h:487: Warnung: `long unsigned int McbDESImpl<0>::m_cbResult'
tDES.h:133: Warnung: während es hier initialisiert wurde
make: *** [.obj/tDES.o] FehlerHier mal ein Auszug aus dem Source, wo der erste Error auftrat:
template <int n> void McbDESImpl<n>::McbCreateSubKeys( const unsigned char Key[8], unsigned char SubKeys[16][6]) { /* ************************************************************************* * table which dicates the number of left shifts required for the pairing ************************************************************************* */ static unsigned char lShifts[] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; /* ************************************************************************* * table used to permutate ************************************************************************* */ static unsigned char tblPC2[] = { 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 }; long lPair[17][2]; int n; // <-- ZEILE 668 unsigned char bytePair[7]; unsigned char KeyPlus[7]; /* ************************************************************************* * create permutated key (K+) ************************************************************************* */ McbCreateKeyPlus(Key, KeyPlus); /* ************************************************************************* * Obtain first pair in long format (C0 in [0][0], D0 in [0][1]) ************************************************************************* */ lPair[0][0] = (KeyPlus[0] << 20) | (KeyPlus[1] << 12) | (KeyPlus[2] << 4) | ((KeyPlus[3] & 0xF0) >> 4); lPair[0][1] = ((KeyPlus[3] & 0x0F) << 24) | (KeyPlus[4] << 16) | (KeyPlus[5] << 8) | KeyPlus[6] ; /* ************************************************************************* * Create pairings (Cx, Dx) by left shifting bits from the previous pair * by either one or two shifts (depending on lShifts table). ************************************************************************* */ for (n = 1; n < 17; n++) { if (lShifts[n-1] == 2) { McbRotate26BitsLeft2(lPair[n-1][0], lPair[n][0]); McbRotate26BitsLeft2(lPair[n-1][1], lPair[n][1]); } else { McbRotate26BitsLeft1(lPair[n-1][0], lPair[n][0]); McbRotate26BitsLeft1(lPair[n-1][1], lPair[n][1]); } } /* ************************************************************************* * clear the subkeys array ************************************************************************* */ memset(SubKeys, 0, 16*6); /* ************************************************************************* * create the subkeys array using the pairing permutated with table PC2 ************************************************************************* */ for (n=0; n < 16; n++) { /* ********************************************************************* * convert long pair into byte pair ********************************************************************* */ McbGetKeyPair(bytePair, lPair[n+1][0], lPair[n+1][1]); /* ********************************************************************* * permutate the bits from the byte pair into the subkey ********************************************************************* */ McbMapTrueBits(tblPC2, sizeof(tblPC2), bytePair, SubKeys[n]); } }/* McbCreateSubKeys */Bemerkung: Das ist nicht mein Source. Hat unter Windows ja auch immer schön geklappt, aber mit g++ scheints nicht zu laufen.
Weiß jemand Rat?
MfG
-
Weiß jemand Rat?
Dein Compiler hat dir das Problem doch schon verraten.
Du hast verkürzt folgende Situation:template <int n> void f() { int n = 0; }Das Problem: Du deklarierst ein neues n in f obwohl dort bereits ein n (nämlich der Templateparameter existiert). Das ist genauso falsch wie:
void f() { int n = 0; int n = 0; }Lösung: Einfach eines der beiden ns umbennen:
template <int n> void f() { int m = 0; }
-
Danke. Hab es so gelöst.
Mich wunderts nur, dass der Visual Studio Compiler da nich meckert.
MFG