1

The description of the problem is quite easy... I have an enum placed inside a template class (which I prefer it to be) and for my application i need to be able to define the operator>>() function for this enum...

This however produces a problem in Visual Studio where the Microsoft C/C++ Optimizing Compiler stops working. In other words: "An internal error has occured in the compiler"

Example code which reproduces the error:

#include <iostream>
#include <stdexcept>

template <typename T>
struct S{
    enum X { X_A, X_B, X_C };
    template <typename U>
    friend std::istream& operator>>(std::istream& in, enum S<U>::X& x);
};

template <typename U>
std::istream& operator>>(std::istream& in, enum S<U>::X& x)
{
    int a;
    in >> a;
    x = S::X(a);
    return in;
}

int main()
{
    S<int> s;
    S<int>::X x = S<int>::X_A;
    std::cout << "Input: ";
    std::cin >> x;
    std::cout << "Output: " << x << std::endl;
}

Any help with solving this problem would be much appreciated! I would myself guess that because the class is templated the enum becomes defined multiple times somehow...

Stonegoat
  • 73
  • 5

2 Answers2

0

test.cpp: In function ‘std::istream& operator>>(std::istream&, enum S::X&)’:
test.cpp:16: error: ‘template struct S’ used without template parameters

You need to change x = S::X(a); to x = S<U>::X(a).

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thanks for your response; you are correct But unfortunately that does not keep the compiler from crashing – Stonegoat Jan 09 '12 at 13:06
0

This seems to be working:

 #include <iostream>

    template< typename T >
    struct S
    {
      enum X
      {
       X_A, X_B, X_C
      };

      friend std::istream& operator>>( std::istream& in, typename S< T >::X & x )
      {
       int a;
       in >> a;
       x = S< T >::X( a );

       return in;
      }
    };

    int main( void )
    {
     S< int > s;
     S< int >::X x = S< int >::X_A;
     std::cout << "Input: ";
     std::cin >> x;
     std::cout << "Output: " << x << std::endl;     
     return( 0 );
    }
lapk
  • 3,838
  • 1
  • 23
  • 28