VS 2003中的显式接口实现编译器错误? [英] Explicit interface implementation compiler bug in VS 2003?

查看:53
本文介绍了VS 2003中的显式接口实现编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有两个程序集(一个C#和一个C ++),C ++将
引用到C#。以下代码在VS

2002下编译并运行良好! VS 2003 C ++编译器报告错误


"错误

2555:''TestNamespace :: ClassB :: IInterfaceB.get_Content'':

覆盖虚函数返回类型不同,并且是''测试名空间:: ClassA :: get_Content''>

显式实现IInterfaceA.Content的描述在
中ClassA避免了编译错误。


两个问题:

为什么在C ++中出现此错误(并且C#没有)?

为什么显式实现解决?问题?

C#汇编内容:

-----------------------

使用系统;


命名空间TestNamespace

{

公共接口IInterfaceA

{

string内容

{

get;

}

}


公共接口IInterfaceB

{

int内容

{

get ;

}

}

公共类ClassA:IInterfaceA

{


// string IInterfaceA.Content

// {

// get {return"" ;; }

//}


公共字符串内容

{

get {return"" ;; }

}

}


公共类ClassC:ClassA,IInterfaceB

{

int IInterfaceB.Content

{

get {return 0; }

}

}

}

-------------- ----------

C ++程序集的内容:

------------------- -----

#pragma一次


命名空间TestNamespace

{


public __gc class ClassB:public ClassA,public

IInterfaceB

{

public:


__property int IInterfaceB :: get_Content()

{

返回0;

}


} ;


}

------------------------

Assumed two assemblies (one C# and one C++), C++ refers
to C#. The follwing code compiles and works well under VS
2002! VS 2003 C++ compiler reports the error

"error
2555: ''TestNamespace::ClassB::IInterfaceB.get_Content'':
overriding virtual function return type differs and is
not covariant from ''TestNamespace::ClassA::get_Content''"

Explicit implementation of "IInterfaceA.Content" in
ClassA avoids the compiler error.

Two questions:
Why does this error occur in C++ (and with C# not)?
Why does explicit implementation "solve" the problem?
content of C# assembly:
-----------------------
using System;

namespace TestNamespace
{
public interface IInterfaceA
{
string Content
{
get;
}
}

public interface IInterfaceB
{
int Content
{
get;
}
}
public class ClassA : IInterfaceA
{

// string IInterfaceA.Content
// {
// get { return ""; }
// }

public string Content
{
get { return ""; }
}
}

public class ClassC : ClassA, IInterfaceB
{
int IInterfaceB.Content
{
get { return 0; }
}
}
}
------------------------
content of C++ assembly:
------------------------
#pragma once

namespace TestNamespace
{

public __gc class ClassB : public ClassA, public
IInterfaceB
{
public:

__property int IInterfaceB::get_Content()
{
return 0;
}

};

}
------------------------

推荐答案

HI Martin,


感谢您在小组中发帖!

如果您在MSDN上查找了错误C2555的文档,那么

可以找到以下解释:

"虚函数和派生覆盖函数具有相同的

参数列表但返回类型不同。

派生类中的重写函数与基类中的虚函数不同,只有它的返回类型为




所以在你的情况下,VC ++ 7.1编译器无法确定

" __ property int IInterfaceB :: get_Content(){...}"是执行

的TestNamespace :::: IInterfaceB.get_Content或

TestNamespace :: IInterfaceA :: get_Content,它们具有相同的函数名

和没有参数,只与返回类型不同。


顺便说一句,我不熟悉C#,但我认为在C#中压倒一切

派生类中的函数可以与基类

类中的虚函数区别仅仅是它的返回类型。

祝你好运,

Gary Chang

Microsoft在线合作伙伴支持


安全! - www.microsoft.com/security

此帖子原样是按原样提供的。没有保证,也没有赋予任何权利。

--------------------

HI Martin,

Thanks for posting in the group!

If you have looked up the documentation of the error C2555 on MSDN, you
could find the following explanation:
"A virtual function and a derived overriding function have identical
parameter lists but different return types. An overriding function in a
derived class cannot differ from a virtual function in a base class only by
its return type."

So in your situation, the VC++ 7.1 compiler cannot determine whether the
"__property int IInterfaceB::get_Content(){...}" is the implementation of
the TestNamespace::::IInterfaceB.get_Content or
TestNamespace::IInterfaceA::get_Content, they have the same function name
and without parameter, only different from the return type.

BTW, I am not familiar with the C#, but I think in C# an overriding
function in a derived class can differ from a virtual function in a base
class only by its return type.
Best regards,
Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------


你好Gary,


谢谢你的回复。


我理解c ++编译器的错误信息

完全。但它不应该发生,因为使用



__property int IInterfaceB :: get_Content()


在ClassB中我明确表示我想实现

属性内容接口IInterfaceB。


VS 2002的c ++编译器能够理解这个
以及c#编译器(参见ClassC的实现) )。


最奇怪的是,如果我实施IIntefaceA的内容

属性。在ClassA中明确地然后

c ++编译器报告没有错误,尽管导致错误的函数

仍然存在。

Hello Gary,

thank you for your reply.

I understand the error message of the c++ compiler
completely. But it should not occur, because using the
line

__property int IInterfaceB::get_Content()

in ClassB I stated clearly that I want to implement the
property "Content" of the interface "IInterfaceB".

The c++ compiler of VS 2002 was able to understand this
as well as the c# compiler (see implementation of ClassC).

The most strange thing is if i implement the "Content"
property of "IIntefaceA" in ClassA explicitly then the
c++ compiler reports no error although the functions
which have caused the error are still there.


嗨Martin,


感谢您的快速回复!


从我的角度来看,如果你实施了"内容和QUOT;

" IIntefaceA"的财产在ClassA中,编译器可以适合您的实现

属性Content (在VC组装中)仅接口

IInterfaceB,原因是内容和内容。 IIntefaceA的属性

已经有一个明确的实现。


当你明确指出时,我注意到Class View选项卡中有一些不同的东西
$ b < IInterfaceA.Content的$ b实现在ClassA(C#Assembly)。

祝你好运,

Gary Chang

微软在线合作伙伴支持


安全! - www.microsoft.com/security

此帖子原样是按原样提供的。没有保证,也没有赋予任何权利。

--------------------

Hi Martin,

Thanks for your quickly response!

From my point of view, if you implement the "Content" property of
"IIntefaceA" in ClassA explicitly, the compiler can fit your implementation
of the property "Content" (in VC assembly) only to interface
"IInterfaceB", for the reason that "Content" property of "IIntefaceA"
already have a explicit implementation.

I noticed something deferent in the Class View Tab when you explicit
implementation of "IInterfaceA.Content" in ClassA (C# Assembly).
Best regards,
Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------


这篇关于VS 2003中的显式接口实现编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆