C++ 从控制台读取:操作员>&燃气轮机;用于模板类内的枚举

C++ 从控制台读取:操作员>&燃气轮机;用于模板类内的枚举,c++,templates,visual-c++,enums,C++,Templates,Visual C++,Enums,这个问题的描述很简单。。。我在模板类中放置了一个枚举(我更喜欢它),对于我的应用程序,我需要能够为此枚举定义运算符>>()函数 然而,这在VisualStudio中产生了一个问题,Microsoft C/C++优化编译器停止工作。换句话说:“编译器中发生了内部错误” 再现错误的示例代码: #include <iostream> #include <stdexcept> template <typename T> struct S{ enum X {

这个问题的描述很简单。。。我在模板类中放置了一个枚举(我更喜欢它),对于我的应用程序,我需要能够为此枚举定义运算符>>()函数

然而,这在VisualStudio中产生了一个问题,Microsoft C/C++优化编译器停止工作。换句话说:“编译器中发生了内部错误”

再现错误的示例代码:

#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;
}
#包括
#包括
样板
结构{
枚举X{X_A,X_B,X_C};
样板
friend std::istream&operator>>(std::istream&in,枚举S::X&X);
};
样板
std::istream和运算符>>(std::istream和in,枚举S::X和X)
{
INTA;
在>>a;
x=S::x(a);
返回;
}
int main()
{
S S;
S::X=S::X_A;
std::cout>x;
标准::cout
test.cpp:在函数“std::istream&operator>>(std::istream&,enum S::X&)”中:
test.cpp:16:错误:“使用的模板结构没有模板参数”


您需要将
x=S::x(a);
更改为
x=S::x(a)

这似乎有效:

 #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 );
    }
#包括
模板
结构
{
枚举X
{
X_A,X_B,X_C
};
friend std::istream&operator>>(std::istream&in,typename S::X&X)
{
INTA;
在>>a;
x=S:x(a);
返回;
}
};
内部主(空)
{
SS;
S::X=S::X_A;
std::cout>x;

std::cout参数列表中不需要
enum
,但可能
typename S::X&
会更好。这不是不可推断的上下文吗?
typename
阻止了编译器崩溃,但它现在抱怨说,在包含
std::cin>>X的行中找不到任何操作符,这与问题a类似bout
运算符谢谢您的回答;您是正确的,但不幸的是,这并不能阻止编译器崩溃谢谢您!:)语法看起来很奇怪,但我喜欢;)@KerrekSB我在帮助解决语法问题,而不是实现。