[ SpreX @ 19.06.2011. 13:22 ] @
Code: #include <iostream> #include <string> #include <map> using namespace std; class Student { public: Student(); Student(const string& name, const int age); Student(const Student& rhs); ~Student() {} void SetName(const string& name) { itsName=name; } string GetName() const { return itsName; } void SetAge(const int age) { itsAge=age; } int GetAge() const { return itsAge; } Student& operator=(const Student& rhs); private: string itsName; int itsAge; }; Student::Student(): itsName("Novi djak"), itsAge(16) {} Student::Student(const string& name, const int age): itsName(name), itsAge(age) {} Student::Student(const Student& rhs): itsName(rhs.GetName()), itsAge(rhs.GetAge()) {} Student& Student::operator=(const Student& rhs) { itsName=rhs.GetName(); itsAge=rhs.GetAge(); return *this; } ostream& operator<<(ostream& os, const Student& rhs) { os << rhs.GetName() << " ima " << rhs.GetAge() << " godina.\n"; return os; } template <class T, class A> void ShowMap(const map<T, A>& v); //prikazuje svojstva kontejnerske klase map typedef map<string, Student> SchoolClass; // <kljuc=ime stud., vrednost=Stud.> int main() { Student Harry("Harry",18); Student Sally("Sally",15); Student Bill("Bill", 17); Student Peter("Peter", 16); SchoolClass MathClass; //map<string, Student> MathClass; //dodavanje clana kod "map" kontejnera //map_object[key_valye]=object_value; MathClass[Harry.GetName()]=Harry; MathClass[Sally.GetName()]=Sally; MathClass[Bill.GetName()]=Bill; MathClass[Peter.GetName()]=Peter; cout << "MathClass:\n"; ShowMap(MathClass); cout << "Zna se da " << MathClass["Bill"].GetName() << " ima "; cout << MathClass["Bill"].GetAge() << " godina.\n"; return 0; } //prikazuje svojstva kontejnerske klase map template <class T, class A> void ShowMap(const map<T, A>& v) { for (map<string, Student>::const_iterator ci=v.begin(); ci!=v.end(); ++ci) cout << ci->first << ": " << ci->second << "\n"; cout << endl; } Program od gore radi, medjutim kada u funkciji ShowMap zamenim Code: map<string, Student>::const_iterator sa Code: map<T, A>::const_iterator kako bi mogli i drugi tipovi da budu prihvaceni dobijam gresku Code: lst19_10_Kontejnerska_klasa_map.cxx: In function ‘void ShowMap(const std::map<T, A, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >&)’: lst19_10_Kontejnerska_klasa_map.cxx:90: error: expected ‘;’ before ‘ci’ lst19_10_Kontejnerska_klasa_map.cxx:90: error: ‘ci’ was not declared in this scope lst19_10_Kontejnerska_klasa_map.cxx: In function ‘void ShowMap(const std::map<T, A, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, A = Student]’: lst19_10_Kontejnerska_klasa_map.cxx:75: instantiated from here lst19_10_Kontejnerska_klasa_map.cxx:90: error: dependent-name ‘std::map::const_iterator’ is parsed as a non-type, but instantiation yields a type lst19_10_Kontejnerska_klasa_map.cxx:90: note: say ‘typename std::map::const_iterator’ if a type is meant Postoji li nacin da se to uradi i kako? |