Hello,
Nice to have the list members back on the net,
I have a problem with some Standard template Library code examples.
It looks nice on the paper, but my GCC 3.3.2 womits.
I have consulted a nice tutorial introduction about templates and especially STL,
so the code is from here :
http://www.libertyassociates.com/pages/files/TYCPP4eSource2.zip
Please help here are the error messages:
4eList1910_UNIX.cpp:
In function
`void ShowMap(const std::map<T, A, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >&)':
4eList1910_UNIX.cpp:
107: error:
parse error before `=' token
//
// Display map properties
//
template<class T, class A>
void ShowMap(const map<T, A>& v)
{
for (map<T, A>::const_iterator ci = v.begin(); // line 107
/*=======================!!=========================*/
ci != v.end(); ++ci) cout << ci->first << ": " << ci->second << "\n";
cout << endl; }
4eList1910_UNIX.cpp:
In function
`void ShowMap(const std::map<T, A, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >&)
[with T = std::string, A = Student]':
4eList1910_UNIX.cpp:93: instantiated from here
template<class T, class A> void ShowMap(const map<T, A>& v); // display map properties
typedef map<string, Student> SchoolClass;
ShowMap(MathClass); // Line 93
4eList1910_UNIX.cpp:107: error: `ci' undeclared (first use this function)
4eList1910_UNIX.cpp:107: error: (Each undeclared identifier is reported only
once for each function it appears in.)
g++ -v
Reading specs from /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/specs
Configured with: ../gcc-3.3.2/configure --prefix=/opt/sfw/gcc-3 --with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as --without-gnu-ld --without-gnu-as --enable-shared
Thread model: posix
gcc version 3.3.2
I compiled and linked without any makefile, using these options
g++ -ansi -pedantic -Wall -o 4eList1910_UNIX.out 4eList1910_UNIX.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++
obviously the code is not free from warnings and not ANSI compatible, but if I use these options
g++ -o 4eList1910_UNIX.out 4eList1910_UNIX.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++
4eList1910_UNIX.cpp: In function `void ShowMap(const std::map<T, A,
std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >&)':
4eList1910_UNIX.cpp:107: warning: `std::map<T, A, std::less<_Key>,
std::allocator<std::pair<const _Key, _Tp> > >::const_iterator' is implicitly
a typename
4eList1910_UNIX.cpp:107: warning: implicit typename is deprecated, please see
the documentation for details
without the command line -options -ansi -pedantic -Wall
the code compiles, links and actually runs.
Please help.
How can I improve the code, in order to make it unambiguous for gcc 3.3.2 ?
Yours Sincerely
Morten Gulbrandsen
#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); string GetName() const; void SetAge(const int age); int GetAge() const; Student& operator=(const Student& rhs); private: string itsName; int itsAge; }; Student::Student() : itsName("New Student"), 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() {} void Student::SetName(const string& name) { itsName = name; } string Student::GetName() const { return itsName; } void Student::SetAge(const int age) { itsAge = age; } int Student::GetAge() const { return itsAge; } Student& Student::operator=(const Student& rhs) { itsName = rhs.GetName(); itsAge = rhs.GetAge(); return *this; } ostream& operator<<(ostream& os, const Student& rhs) { os << rhs.GetName() << " is " << rhs.GetAge() << " years old"; return os; } template<class T, class A> void ShowMap(const map<T, A>& v); // display map properties typedef map<string, Student> SchoolClass; int main() { Student Harry("Harry", 18); Student Sally("Sally", 15); Student Bill("Bill", 17); Student Peter("Peter", 16); SchoolClass MathClass; MathClass[Harry.GetName()] = Harry; MathClass[Sally.GetName()] = Sally; MathClass[Bill.GetName()] = Bill; MathClass[Peter.GetName()] = Peter; cout << "MathClass:\n"; ShowMap(MathClass); cout << "We know that " << MathClass["Bill"].GetName() << " is " << MathClass["Bill"].GetAge() << " years old\n"; return 0; } // // Display map properties // template<class T, class A> void ShowMap(const map<T, A>& v) { for (map<T, A>::const_iterator ci = v.begin(); ci != v.end(); ++ci) cout << ci->first << ": " << ci->second << "\n"; cout << endl; } /* g++ -ansi -pedantic -Wall -o 4eList1910_UNIX.out 4eList1910_UNIX.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++ 4eList1910_UNIX.cpp: In function `void ShowMap(const std::map<T, A, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >&)': 4eList1910_UNIX.cpp:107: error: parse error before `=' token 4eList1910_UNIX.cpp: In function `void ShowMap(const std::map<T, A, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >&) [with T = std::string, A = Student]': 4eList1910_UNIX.cpp:93: instantiated from here 4eList1910_UNIX.cpp:107: error: `ci' undeclared (first use this function) 4eList1910_UNIX.cpp:107: error: (Each undeclared identifier is reported only once for each function it appears in.) g++ -v Reading specs from /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/specs Configured with: ../gcc-3.3.2/configure --prefix=/opt/sfw/gcc-3 --with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as --without-gnu-ld --without-gnu-as --enable-shared Thread model: posix gcc version 3.3.2 */