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

This is a discussion on Works in MSVC++ 6.0 but not in Dev-C++ within the C++ Programming forums, part of the General Programming Boards category; 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. ...

  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,676
    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, 07:47 PM
  2. openGL programming - From MSVC++ to Dev C++
    By WDT in forum Game Programming
    Replies: 1
    Last Post: 03-08-2004, 04:19 PM
  3. Visual Studio 6.0 Help
    By L_I_Programmer in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2003, 09: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, 07: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

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