Thread: problem with tempates in devc++

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    problem with tempates in devc++

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    map<T, A>::const_iterator is a name dependent on the template parameter T. As such, you should prefix the name with typename to denote that map<T,A>::const_iterator is a type name:
    Code:
    for(typename map<T, A>::const_iterator ci = v.begin();
    Some compilers allow typename to be omitted in this context, but that is non-standard. In the absence of the typename keyword, the name may be interpreted as a non-type name (e.g., a variable name).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm tempted to follow the recommended c++ books on the www.cprogramming.com book review page

    I personally wouldn't recommend it. Some of those books are good, some aren't. They are just the opinion of the owner of the site. I would recommend against C++ Without Fear.

    I'd check the book recommendation thread and see which books there get positive comments from multiple people and sound like something for you. My opinion is that Accelerated C++ would be a better book to move to, and then some of the intermediate/advanced books after that if you still want references and more detail.

    As for your error, I think you need the typename keyword before map<T, A>::const_iterator ci. I'm surprised VC++ 2008 allows this to compile, although that might be due to backwards compatibility concerns.
    Last edited by Daved; 04-10-2008 at 10:41 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Visual C++ has never required the "typename" keyword for such purposes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    excellent, thanks everyone for the clarification, I can see what was going wrong now!
    Daved, thanks for recommendation, I'll be sure to check out the thread and the book you recommended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM