weird compile error

This is a discussion on weird compile error within the C++ Programming forums, part of the General Programming Boards category; Code: template <class C, class T> int count2(const C &v, T val) { C::const_iterator i = find(v.begin(), v.end(), val); int ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    weird compile error

    Code:
    template <class C, class T> int count2(const C &v, T val)
    {
        C::const_iterator i = find(v.begin(), v.end(), val);
        int n = 0;
        while(i != v.end()) {
            ++n;
            ++i;
            i = find(i, v.end(), val);
        }
        return n;
    }
    This functions counts the number of occurences of 'val' inside the type C.

    What i dont understand is why do i get compile error for this that i is undeclared...
    Last edited by Tool; 06-23-2010 at 04:15 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,989
    The problem that I see is that C::const_iterator is a name dependent on the template parameter C, hence it should be disambiguated with typename otherwise it would be assumed to be a member name instead of a type name, i.e.,
    Code:
    typename C::const_iterator i = find(v.begin(), v.end(), val);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Mind you, if this isn't merely an exercise, there's a std::count that already does what you want.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21