error C2664 是什么意思,怎么解决呢?

simalaya 2004-06-18 02:48:39
y0Dlg.cpp
C:\y0\y0Dlg.cpp(213) : error C2664: 'GetItem' : cannot convert parameter 1 from 'const int' to 'const struct tagVARIANT &'
Reason: cannot convert from 'const int' to 'const struct tagVARIANT'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.

y0Dlg.obj - 1 error(s), 0 warning(s)
...全文
1808 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjsunjifu 2004-06-22
  • 打赏
  • 举报
回复
Technote: Improved Conformance to ANSI C++
As a result of improved C++ conformance, some constructs that used to compile will now give errors. Each error message that may result from some of these changes is flagged by "(new behavior; please see help)" at the end of the message. This doesn't mean that the error message has a new meaning; it means that the compiler may be diagnosing an error which it previously did not.

The errors affected are:

C2434 C2440 C2446 C2553 C2585 C2592
C2664 C2665 C2666 C2667 C2668 C2674
C2675 C2676 C2677 C2678 C2679


Overload Resolution Changes
Visual C++ overload resolution rules have been brought more in line with those specified in the draft C++ standard. This will allow some things to work that previously did not, but it also will result in ambiguities in code that compiled cleanly with earlier versions of Microsoft Visual C++.

Dereferencing a Reference is an Identity Conversion

Previous versions of Microsoft Visual C++ considered dereferencing a reference or binding to a reference to be a worse conversion than an exact match. These conversions (listed in the Annotated C++ Reference Manual as trivial conversions) are now considered to be identity.

For example:

void foo(const int);
void foo(int&);

void bar(void)
{
int i;
int &ri = i;
foo(i); // Accepted by VC2.0, VC1.x, rejected by VC4.0, error C2668
foo(ri); // Accepted by VC2.0, VC1.x, rejected by VC4.0, error C2668
}

User-Defined Conversions (UDCs) are Selected by 'this' Pointer Types

Previous versions of Visual C++ selected the user-defined conversion to call based on the conversion required to get from the result type of the UDC to the required type and ignored the conversion required for the 'this' pointer of the UDC. User-defined conversions are now selected based on the quality of match on the 'this' pointer. As a Microsoft extension (not currently flagged by /Za), the quality of the conversion from the UDC result to the target type is used as a tie-breaker in the event that the 'this' pointers match.

This change of behavior will catch some bad errors that previously had not been detected. For example:

struct C {
// some class
};

struct D {
// A class which wraps a C
C myC;

// Two user-defined conversion functions, one for
// using myC, and the other for modifying it
operator C () { return myC; } // conversion #1
operator C& () { return myC; } // conversion #2
};

// A function that takes a C
void func1(C);

// A function that has a D
void func2()
{
D aD;

func1(D); // Error diagnosed in VC4.0: error C2664
}

The error in this case is that the user-defined conversion is ambiguous. This is because the conversion for the this-pointer is identical for both UDCs, and the result of the UDCs has an identical conversion. The following example (added to the previous one) illustrates another error:

// A function that wants to modify a C
void func3(C&);

// Some function that gets a D&
void func4(const D& aD)
{
func3(aD); // Error diagnosed in VC4.0: error C2664
}

This time, the error occurs because neither UDC can be called—the D object is const, but neither UDC takes a const this pointer.

The way to correct both these problems is to rewrite conversion #1 as follows:

operator C () const { return myC; } // conversion #1a

or:

operator const C& () const { return myC; } // conversion #1b

Both these conversions take a const this pointer, so they can be called for const objects. In addition, conversion #1b returns a reference to a const object, avoiding copy construction, but still ensuring that the original object will not be modified.

An additional, more subtle effect of this change is that user-defined conversions can silently change behavior. For example:

void foo(int);

struct C {
operator int () const;
operator long ();
};

void main(void)
{
C aC;
const C acC;

foo(acC); // calls "operator int() const"
foo(aC); // calls "operator int() const" in VC2.0,
// but "operator long()" in VC4.0
}
simalaya 2004-06-18
  • 打赏
  • 举报
回复
源代码是:
h_root = hr.GetItem(0);

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧