Ahh, C++ mẫu là gì ...ý nghĩa của 'thiếu mẫu tranh cãi' C++ lỗi này
The code I see,
makes sense to me,
but GCC...
it disagrees.
Các mã sau biên dịch và chạy như mong đợi, nhưng nếu bạn bỏ ghi chú rằng #define
, bạn nhận được lỗi mà tôi không hiểu. Biểu tượng iterator
vẫn chỉ có một thứ mà nó có thể tham chiếu đến: typedef trong siêu lớp. Vì vậy, tôi đoán tôi có hai câu hỏi: 1. Các lỗi có ý nghĩa gì? 2. Cách tốt nhất để khắc phục chúng là gì.
#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"));
}
lỗi từ 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)
Bạn đã thử chuỗi :: iterator? – Lordalcol