Thread: Works in MSVC++ 6.0 but not in Dev-C++

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Question Works in MSVC++ 6.0 but not in Dev-C++

    Hi. Does anyone have any idea why the following code works in MSVC++ 6.0 but not in Dev-C++ 4.9.8.0? Thanks.
    Code:
    #include<iostream>
    #include<iterator>
    #include<vector>
    #include<string>
    
    using namespace std;
    
    template <class T>
    class Set
    {
      public:
        void add(const T& anItem);
        bool has(const T& targetItem);
        friend ostream& operator <<(ostream& os, Set<T>& aSet)
        {
          vector<T>::iterator anIt;
          for(anIt=aSet.theMembers.begin();anIt != aSet.theMembers.end();anIt++)
          {
            os<<(*anIt)<<" ";
            os<<endl;
          }
    	  return os;
        }
      private:
        vector<T> theMembers;
    };
    
    int main()
    {
      Set<int> s1;
      s1.add(10);
      cout<<s1;
      cout<<s1.has(10)<<endl;
      system("pause");
      return 0;
    }
    
    template <class T>
    void Set<T>::add(const T& anItem)
    {
      theMembers.push_back(anItem);
    }
    
    template<class T>
    bool Set<T>::has(const T& targetItem)
    {
      vector<T>::iterator anIt;
      
      for(anIt=theMembers.begin(); anIt != theMembers.end(); anIt++)
      {
        if((*anIt)==targetItem)
          return true;
      }
        return false;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Change all occurences of
    Code:
    vector<T>::iterator anIt;
    to
    Code:
    typename vector<T>::iterator anIt;
    The standard says that any type which is dependent on a template paramater, must be prefixed by the keyword typename.

    The type of "anIt" is "vector<T>::iterator". This type is dependent on the template parameter T, so you have to use typename in front of it.

    >> ... why the following code works in MSVC++ 6.0 but not in Dev-C++ 4.9.8.0?
    Because gnu/g++ is a more standards conforming compiler.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. openGL programming - From MSVC++ to Dev C++
    By WDT in forum Game Programming
    Replies: 3
    Last Post: 03-08-2004, 08:47 PM
  2. openGL programming - From MSVC++ to Dev C++
    By WDT in forum Game Programming
    Replies: 1
    Last Post: 03-08-2004, 05:19 PM
  3. Visual Studio 6.0 Help
    By L_I_Programmer in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2003, 10:35 PM
  4. Works on Dev C++, Doesn't on Visual C++ :(
    By marcvb in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2001, 08:39 AM
  5. SDI Menu problem - Windows MSVC 6.0
    By Brown Drake in forum C++ Programming
    Replies: 0
    Last Post: 10-13-2001, 06:04 AM