Okay, I've been learning C++ using SAMS Learn C++ in 21 days (5th edition) for just over a month, which i see people have mixed opinions about.
I've worked through the book, largely without any problems, and have a reasonably good understanding of the basics now, the only areas I'm a little hazy on are friend functions, unnamed namespaces and templates, but I think that's more to do with the way they're explained (ie: not properly explained) by the author.
I'm currently working on Day 19, "Templates" and a lot of the examples will only compile using MS VC++ 2008 express and will always produce some kind of compiler error using the DevC++ compiler.
My experience with templates thus far seems to be that they work largely without issue on MS compilers yet other compilers seem to struggle if using an STL or user defined template that takes more that one object, ie: the "map" container.
For example, Listing 19.13 (shown below) produces the following error on the latest wxDevCPP 6.10.2, insisting that the constant iterator (ci) is undeclared, yet compiles without issue on MS V C++ express 08. I've also had a myriad of other compiler related errors when working with templates in DevC++.
Can anyone shed some light as to what is going on here? Is my DevCPP compiler out of date or is the MS compiler the only version with decent template support?
Lastly, as I've nearly finished my current book I'm trying to decide where to go next, I'm tempted to follow the recommended c++ books on the www.cprogramming.com book review page, starting with Brian Overland's C++ without fear book to solidify my knowledge and plug any gaps left by the SAMS book and working onwards through "Practical C++ Programming" by Steve Ouallineand and through the other books from there.
As a new C++ programmer, (though I used to use VB) it's kind of difficult to gauge my own ability, but I have been irritated by the way the latter 1/3 of the SAMS book is written and some of the examples ARE wrong.
Is "C++ without Fear" better written than the Sams C++ 21 Days (and is it necessary if I've already understood MOST of whats in the SAMs book?) or should I just move onto the next book in the recommended books list?
Many thanks,
Code:
/////////////////////////////////////////
//Listing 19.10 - A map container class
#include <iostream>
#include <string>
#include <map> //used to create map containers
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: " << endl;
ShowMap(MathClass);
cout << "We know that " << MathClass["Bill"].GetName()
<< " is " << MathClass["Bill"].GetAge()
<< " years old" << endl;
system("PAUSE"); return EXIT_SUCCESS;
}
//
//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 << endl;
cout << endl;
}
Code:
/////////////////////////////////////
//ERROR PRODUCED BY DEVCPP:
C:\Program Files\Dev-Cpp\Z Projects\Day 19\19.10 - A Map container class.cpp In function `void ShowMap(const std::map<T, A, std::less<_Key>, std::allocator<std::pair<const _Key, _Tp> > >&)':
107 C:\Program Files\Dev-Cpp\Z Projects\Day 19\19.10 - A Map container class.cpp expected `;' before "ci"
108 C:\Program Files\Dev-Cpp\Z Projects\Day 19\19.10 - A Map container class.cpp `ci' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
C:\Program Files\Dev-Cpp\Z Projects\Day 19\19.10 - A Map container class.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]':
93 C:\Program Files\Dev-Cpp\Z Projects\Day 19\19.10 - A Map container class.cpp instantiated from here
107 C:\Program Files\Dev-Cpp\Z Projects\Day 19\19.10 - A Map container class.cpp dependent-name ` std::map<T,A,std::less<_Key>,std::allocator<std::pair<const _Key, _Tp> > >::const_iterator' is parsed as a non-type, but instantiation yields a type
say `typename std::map<T,A,std::less<_Key>,std::allocator<std::pair<const _Key, _Tp> > >::const_iterator' if a type is meant
C:\Program Files\Dev-Cpp\Z Projects\Day 19\Makefile.win [Build Error] exe: *** [Objects/MingW/19.10 - A Map container class.o] Error 1