Microsoft KB Archive/147427

= DOC: Description of C2354 Error Is Unclear =

Article ID: 147427

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 2.2
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q147427



SUMMARY
Neither the error message itself nor the online documentation for the C2354 compiler error clearly specify the cause of this error:

'reference' : initialization of reference member requires a temporary variable



MORE INFORMATION
It is illegal to initialize a reference member of a class in the class's constructor with a temporary variable. An attempt to do so generates the C2354 error, as illustrated by this sample code:

Sample Code
int temp { return 1; }

class Test { public: int member; int& ref_member; Test; };

Test::Test : ref_member( temp ) { } When this error is encountered, the solution is to change the code so that the reference member is not initialized to a temporary variable. The reference must be initialized to an object that will exist for the lifetime of the reference member.

Keywords: kbbug kbdocerr kbcpponly kbcompiler kboldocs KB147427

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.