Frage

Ich bin Portierung einig Templat-Code von Windows-über und ich habe einige Compiler Unterschiede auf dem iPhone 3.2 SDK zu treffen.

Original-Code innerhalb einer Elementfunktion der Klasse Vorlage:

return BinarySearch<uint32, CSimpleKey<T> >(key);

Dabei gilt Binary eine Methode aus einer anderen Vorlage.

geerbt ist

Dies erzeugt die folgenden Fehler:

csimplekeytable.h:131: error: no matching function for call to 'BinarySearch(NEngine::uint32&)'

Das Visual Studio Compiler scheint die Vorlage Hierarchie in Ordnung zu gehen, aber gcc braucht mich zu qualifizieren zu voll, wo die Funktion herkommt (ich dies überprüft habe, durch die gleichen Probleme mit Template-Membervariablen auf dieser Weise Fixing).

Also habe ich jetzt brauche dies in ändern:

return CSimpleTable<CSimpleKey<T> >::BinarySearch<uint32, CSimpleKey<T> >(key);

Welche der folgenden Fehler jetzt erzeugt:

csimplekeytable.h:132: error: expected primary-expression before ',' token
csimplekeytable.h:132: error: expected primary-expression before '>' token

Nach einigem Kopf kratzen, ich glaube, hier was los ist, dass es versucht, den ‚<‘ vor Binary als ‚weniger als‘ Betreiber aus irgendeinem Grunde zu lösen.

So zwei Fragen: - Bin ich auf dem richtigen Weg mit meiner Interpretation des Fehlers? - Wie kann ich das Problem beheben

?

D

War es hilfreich?

Lösung

If CSimpleTable is the base class, you need to qualify your call with that base class name or alternatively with this. But since both of these depend on the template parameters, the compiler cannot lookup what the name BinarySearch means. It could be a static integer constant, which you compare against something else, or it could be a template that you put arguments enclosed in <...> for. You need to tell the compiler about the latter

/* the "::template" means: 'the name that follows is a template' */
return CSimpleTable<CSimpleKey<T> >::template BinarySearch<uint32, CSimpleKey<T> >(key);

Or with this

return this->template BinarySearch<uint32, CSimpleKey<T> >(key);

The reason for the qualification is that the compiler does not look for unqualified names in base classes that depend on template parameters (in your case the parameter is T), since the binding of the name would depend on whether the base class has such a name or not, which is considered unfortunate. For a plain name like BinarySearch, there is in addition no indication that this name depends on a template parameter, thus the Standard requires compilers not to delay lookup of the name until instantiation. So the name, even if lookup in dependent bases would be allowed on instantiation (which isn't), couldn't be found anyway.

You have to explicitly tell the compiler to look in the enclosing class by qualifying it, in which event the compiler will include dependent base classes during lookup when instantiating. Prefixing with this or the class name will also make the name dependent, thus delaying lookup of it until instantiation. These two facts are required for it to work.

Andere Tipps

Maybe it's just a problem with the uint32 typedef. It could be that it's not available in the new environment. In the first case the namespace resolution picks up NEngine::uint32, in the second case it doesn't pick anything at all (hence the parsing error).

I suggest looking for how uint32 is defined on Windows, and try copying that definition over.

Perhaps it's because you've made your spacings inconsistent. You've done CSimpleTable<CSimpleKey<T> >, rather than CSimpleTable<CSimpleKey<T>>. I believe that they should both be valid, but perhaps there's a parser bug involved.

Edit: You could also try heading down the automatic argument type deduction route, and chopping the explicit specialization.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top