我认为我遇到了(可能的)VC6(我知道。这是我们使用的。)编译器错误,但是我容易错过一些愚蠢的事实。给出以下代码(这只是一个示例!):

#include <iostream>

// Class with template member function:
class SomeClass
{
public:
  SomeClass() {};

  template<class T>
  T getItem()
  {
    return T();
  };
};


// Dummy just used to recreate compiler error
class OtherClass
{
public:
  OtherClass() {};
};

std::ostream& operator<<( std::ostream& oStr, const OtherClass& obj )
{
  return oStr << "OtherClass!";
};

// Main illustrates the error:
int main(int argc, char* argv[])
{
  SomeClass a;

  OtherClass inst2 = a.getItem<OtherClass>(); // Error C2275 happens here!
  std::cout << inst2 << std::endl;

  return 0;
}

如果我尝试编译此代码VC6,则死于a.getItem<OtherClass>()并产生:
Error C2275: 'OtherClass' : illegal use of this type as an expression

我是否忽略了一些琐碎的语法问题?我违反规则了吗?
此代码在gcc 4.3.4下可以正常编译。 VC6是否还有另一个合规性问题?

谢谢!

最佳答案

这可能是VC6问题。尽管VC6可以正确编译大多数基本模板,但众所周知,当您开始着手于更高级的模板使用时,会遇到许多问题。成员模板是VC6一致性较弱的区域。

关于c++ - 由模板成员功能引起的错误C2275。此代码是否错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3764340/

10-13 02:56