Ahh, C++ template ...Qual è il significato di questo errore 'mancare argomenti modello' C++
The code I see,
makes sense to me,
but GCC...
it disagrees.
Il seguente codice compilato ed eseguito come previsto, ma se si rimuove il commento che #define
, si ottiene l'errore , che non capisco. Il simbolo iterator
ha ancora una sola cosa a cui può riferirsi: il typedef nella superclasse. Quindi immagino di avere due domande: 1. Che cosa significano gli errori? 2. Qual è il modo migliore per risolverli.
#include <map>
#include <string>
#include <cstdio>
using namespace std;
// #define WITH_TEMPLATE 1
#ifdef WITH_TEMPLATE
template <class C>
struct MyClass : public map<string, C>
#else
struct MyClass : public map<string, int>
#endif
{
bool haskey(const string &s)
{
iterator it = find(s);
return (it != end());
}
};
int main()
{
#ifdef WITH_TEMPLATE
MyClass<int> m;
#else
MyClass m;
#endif
m["test"] = 10;
printf("%d %d\n", m.haskey("test"), m.haskey("no"));
}
Errori da GCC:
temp.cc: In member function ‘bool MyClass::haskey(const std::string&)’:
temp.cc:18: error: missing template arguments before ‘it’
temp.cc:18: error: expected `;' before ‘it’
temp.cc:19: error: ‘it’ was not declared in this scope
temp.cc:19: error: there are no arguments to ‘end’ that depend on a template parameter, so a declaration of ‘end’ must be available
temp.cc:19: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
Hai provato stringa :: iterator? – Lordalcol