源代码可以在 GCC 中编译,但不能在 Visual Studio 中编译

发布于 2024-11-25 09:37:24 字数 480 浏览 6 评论 0原文

这可以在 GCC 4.6 上编译,但不能在 VS2010 sp1 上编译:
是我的错还是VS又搞砸了?

 #include "stdafx.h"

enum Attribute{red_att,black_att,key_att,value_att};

struct Color{};



template<enum Attribute>
struct Tag_Value;

template<>
struct Tag_Value<red_att>
{
    typedef Color type;
};

int main()
{

    return 0;
}

错误:
错误C2599:'Attribute':不允许枚举类型的前向声明

错误C2440:'specialization':无法从'Attribute'转换为'Attribute'

This compiles on GCC 4.6 but doesn't with VS2010 sp1:
Is it my fault or VS screwed up again?

 #include "stdafx.h"

enum Attribute{red_att,black_att,key_att,value_att};

struct Color{};



template<enum Attribute>
struct Tag_Value;

template<>
struct Tag_Value<red_att>
{
    typedef Color type;
};

int main()
{

    return 0;
}

Errors:
error C2599: 'Attribute' : forward declaration of enum type is not allowed

error C2440: 'specialization' : cannot convert from 'Attribute' to 'Attribute'

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

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

发布评论

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

评论(3

萌︼了一个春 2024-12-02 09:37:24

假设一个有效且不冲突的 stdafx.h,看起来像是有效的代码。

您会发现人们告诉您,在 C++ 中,如果没有任何内容隐藏 Name(例如一个名为 Name 的函数)。在 C 中你必须这样做,因为 C 对于查找名字有不同的概念。但在 C++ 中,要引用结构、类、联合或枚举,只需使用 Name 即可。因此,您可以使用 Attribute 而不是 enum Attribute。但是命名类型的不同选择不应使编译器拒绝您的代码。

Assuming a valid and non-conflicting stdafx.h, that looks like valid code.

You will find people tell you that in C++, you don't have to say enum Name or struct Name if nothing is hiding Name (like a function called Name). In C you have to, because C has a different concept for looking up names. But in C++, to refer to a struct, class, union or enum, you can just use Name. So you can use Attribute instead of enum Attribute. But the different choice of naming the type should not make the compiler reject your code.

霞映澄塘 2024-12-02 09:37:24

我不确定确切的问题是什么 - 我的猜测是它将枚举属性视为新枚举声明的开始......也许作为 Tag_Value 的成员。因此,您有两个 Attribute 枚举,并且它不会让您将其中一个与另一个进行专门化。

要解决此问题,只需删除 enum

template<enum Attribute> struct Tag_Value;

即可:

template<Attribute> struct Tag_Value;

I'm not certain what the exact problem is—my guess is that it is treating enum Attribute as the beginning of a new enum declaration... perhaps as a member of Tag_Value. So you have two Attribute enums and it won't let you specialize one with the other.

To fix, just get rid of the enum:

template<enum Attribute> struct Tag_Value;

to this:

template<Attribute> struct Tag_Value;
舂唻埖巳落 2024-12-02 09:37:24

您必须更改此行:

template<enum Attribute> struct Tag_Value; 

为此:

template<Attribute> struct Tag_Value;

假设您想要接受 Attribute 类型的模板参数(枚举是一种类型,是的)。

You'll have to change this line:

template<enum Attribute> struct Tag_Value; 

To this:

template<Attribute> struct Tag_Value;

Assuming you'll want to accept a template argument of type Attribute (an enum is a type, yes).

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