由模板成员函数引起的错误 C2275。这段代码有错吗?

发布于 2024-09-24 12:57:58 字数 1018 浏览 0 评论 0原文

我想我遇到了(可能)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() 上崩溃,生成:

错误 C2275:“OtherClass”:非法使用此类型作为表达式

我是否忽略了一些微不足道的语法问题?我违反规则了吗? 这段代码在 gcc 4.3.4 下编译得很好。这是 VC6 的另一个合规性问题吗?

谢谢!

I think I've run into a (possible) VC6 (I know. It's what we use.) compiler error, but am open to the fact that I've just missed something dumb. Given the following code (It's just an example!):

#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;
}

If I try to compile this code VC6, dies on a.getItem<OtherClass>() yielding:

Error C2275: 'OtherClass' : illegal use of this type as an expression.

Have I overlooked some trivial syntax issue? Am I breaking a rule?
This code compiles just fine under gcc 4.3.4. Is it yet another compliance issue with VC6?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剩余の解释 2024-10-01 12:57:58

除了其中包含“模板”一词的许多其他内容之外,VC6 无法处理模板参数不是函数参数的函数模板。常见的解决方法是添加一个虚拟函数参数:

  template<class T>
  T getItem(T* /*dummy*/ = NULL)
  {
    return T();
  } // note: no ; after function definitions

但是,一般来说,VC6 相当蹩脚,并且一旦 TU 包含 template 关键字,通常就会出现问题。我不得不与它搏斗了好几年(用多个编译器/编译器版本编译的大型代码库;VC6给我们带来了无穷无尽的麻烦),当我在2003年摆脱它时,我感到非常高兴。

Among many other things with the word template in it, VC6 couldn't deal with function templates where the template parameters aren't also function parameters. The common workaround was to add a dummy function parameter:

  template<class T>
  T getItem(T* /*dummy*/ = NULL)
  {
    return T();
  } // note: no ; after function definitions

However, in general, VC6 is pretty lame and often chokes as soon as a TU contains the template keyword. I had to beat my head against it for several years (big code base compiled with several compilers/compiler versions; VC6 giving us an endless amount of trouble) and was very glad when I got rid of it in 2003.

微凉徒眸意 2024-10-01 12:57:58

这可能是 VC6 问题。尽管 VC6 可以正确编译大多数基本模板,但当您开始使用更高级的模板时,它会出现许多问题。众所周知,成员模板是 VC6 在一致性方面较弱的领域。

This is likely to be a VC6 issue. Although VC6 compiles most basic templates correctly it is known to have many issues when you start to move towards the more advanced template uses. Member templates are an area where VC6 is known to be weak on conformance.

聆听风音 2024-10-01 12:57:58

我相信这是 VC6 中的另一个错误,您确实应该切换到更新的编译器。

I believe that's another bug in VC6, you should really switch to a more up-to-date compiler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文