description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid |
---|---|---|---|---|---|
Learn more about: Compiler Error C2039 |
Compiler Error C2039 |
11/04/2016 |
C2039 |
C2039 |
f9dfd521-9b36-4454-a69c-d63f45b606bb |
Compiler Error C2039
‘identifier1’ : is not a member of ‘identifier2’
The code incorrectly calls or refers to a member of a structure, class, or union.
Examples
The following sample generates C2039.
// C2039.cpp struct S { int mem0; } s, *pS = &s; int main() { pS->mem1 = 0; // C2039 mem1 is not a member pS->mem0 = 0; // OK }
The following sample generates C2039.
// C2039_b.cpp // compile with: /clr using namespace System; int main() { Console::WriteLine( "{0}", DateTime::get_Now()); // C2039 Console::WriteLine( "{0}", DateTime::Now); // OK Console::WriteLine( "{0}", DateTime::Now::get()); // OK }
The following sample generates C2039.
// C2039_c.cpp // compile with: /clr /c ref struct S { property int Count { int get(); void set(int i){} }; }; int S::get_Count() { return 0; } // C2039 int S::Count::get() { return 0; } // OK
C2039 can also occur if you attempt to access a default indexer incorrectly. The following sample defines a component authored in C#.
// C2039_d.cs
// compile with: /target:library
// a C# program
[System.Reflection.DefaultMember("Item")]
public class B {
public int Item {
get { return 13; }
set {}
}
};
The following sample generates C2039.
// C2039_e.cpp // compile with: /clr using namespace System; #using "c2039_d.dll" int main() { B ^ b = gcnew B; int n = b->default; // C2039 // try the following line instead // int n = b->Item; Console::WriteLine(n); }
C2039 can also occur if you use generics. The following sample generates C2039.
// C2039_f.cpp // compile with: /clr interface class I {}; ref struct R : public I { virtual void f3() {} }; generic <typename T> where T : I void f(T t) { t->f3(); // C2039 safe_cast<R^>(t)->f3(); // OK } int main() { f(gcnew R()); }
C2039 can occur when you try to release managed or unmanaged resources. For more information, see Destructors and finalizers.
The following sample generates C2039.
// C2039_g.cpp // compile with: /clr using namespace System; using namespace System::Threading; void CheckStatus( Object^ stateInfo ) {} int main() { ManualResetEvent^ event = gcnew ManualResetEvent( false ); TimerCallback^ timerDelegate = gcnew TimerCallback( &CheckStatus ); Timer^ stateTimer = gcnew Timer( timerDelegate, event, 1000, 250 ); ((IDisposable ^)stateTimer)->Dispose(); // C2039 stateTimer->~Timer(); // OK }
title | description | ms.date | ms.custom | ms.reviewer |
---|---|---|---|---|
C2653/C2039 error when you reference STD functions |
Explains that when you try to reference a function from the STD C++ library header <cstdlib>, you may receive a C2653 or a C2039 compiler error message. A workaround is provided in this article. |
04/22/2020 |
sap:C and C++ Libraries |
v-ingor |
C2653 or C2039 error when you try to reference a function from the STD C++ library
This article provides the information about solving the C2653 or C2039 error that occurs when you reference a function from the STD C++ library.
Original product version: Visual C++
Original KB number: 243444
Symptoms
Attempting to reference a function from the STD C++ library header <cstdlib>
using the namespace std
(for example, std::exit(0)
) causes the compiler to emit a C2653 or a C2039 (depending upon whether or not namespace std
is defined at the point where the error is emitted) error message.
Cause
<cstdlib>
does not define the namespace std
. This is contrary to the Visual C++ documentation, which says:
Include the standard header <cstdlib>
to effectively include the standard header <stdlib.h>
within the std
namespace.
Resolution
To work around the problem, place the #include <cstdlib>
in the namespace std
.
More information
Attempting to compile the following will cause the compiler to display the following error:
error C2653: ‘std’ : is not a class or namespace name
// Compile Options: /GX #include <cstdlib> void main() { std::exit(0); }
However, attempting to compile the following causes the compiler to display the following error:
error C2039: ‘exit’ : is not a member of ‘std’
// Compile Options: /GX #include <vector> #include <cstdlib> void main() { std::exit(0); }
In the first case, the C2653 is displayed, because the namespace std
has not been defined. In the second case, the C2039 is displayed, because the namespace std
has been defined (in the header <vector>
), but the function exit
is not part of that namespace. To work around the problem in either case, simply enclose the #include <cstdlib>
in the namespace std
, as follows:
// Compile Options: /GX namespace std { #include <cstdlib> }; void main() { std::exit(0); }
title | description | ms.date | ms.custom | ms.reviewer |
---|---|---|---|---|
C2653/C2039 error when you reference STD functions |
Explains that when you try to reference a function from the STD C++ library header <cstdlib>, you may receive a C2653 or a C2039 compiler error message. A workaround is provided in this article. |
04/22/2020 |
sap:C and C++ Libraries |
v-ingor |
C2653 or C2039 error when you try to reference a function from the STD C++ library
This article provides the information about solving the C2653 or C2039 error that occurs when you reference a function from the STD C++ library.
Original product version: Visual C++
Original KB number: 243444
Symptoms
Attempting to reference a function from the STD C++ library header <cstdlib>
using the namespace std
(for example, std::exit(0)
) causes the compiler to emit a C2653 or a C2039 (depending upon whether or not namespace std
is defined at the point where the error is emitted) error message.
Cause
<cstdlib>
does not define the namespace std
. This is contrary to the Visual C++ documentation, which says:
Include the standard header <cstdlib>
to effectively include the standard header <stdlib.h>
within the std
namespace.
Resolution
To work around the problem, place the #include <cstdlib>
in the namespace std
.
More information
Attempting to compile the following will cause the compiler to display the following error:
error C2653: ‘std’ : is not a class or namespace name
// Compile Options: /GX #include <cstdlib> void main() { std::exit(0); }
However, attempting to compile the following causes the compiler to display the following error:
error C2039: ‘exit’ : is not a member of ‘std’
// Compile Options: /GX #include <vector> #include <cstdlib> void main() { std::exit(0); }
In the first case, the C2653 is displayed, because the namespace std
has not been defined. In the second case, the C2039 is displayed, because the namespace std
has been defined (in the header <vector>
), but the function exit
is not part of that namespace. To work around the problem in either case, simply enclose the #include <cstdlib>
in the namespace std
, as follows:
// Compile Options: /GX namespace std { #include <cstdlib> }; void main() { std::exit(0); }
After upgrading from Visual Studio 2019 to Visual Studio 2022 RC3 I get the following compiler errors for my solution:
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexmemory(154,5): error C2039: '_invalid_parameter': is not a member of '`global namespace''
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexmemory(154,5): error C3861: '_invalid_parameter': identifier not found
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexmemory(164,5): error C2039: '_invalid_parameter': is not a member of '`global namespace''
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexmemory(164,5): error C3861: '_invalid_parameter': identifier not found
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexmemory(1203,9): error C2039: '_invalid_parameter': is not a member of '`global namespace''
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexmemory(1203,1): error C3861: '_invalid_parameter': identifier not found
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includeatomic(206,9): error C2039: '_invalid_parameter': is not a member of '`global namespace''
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includeatomic(206,9): error C3861: '_invalid_parameter': identifier not found
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includeatomic(221,9): error C2039: '_invalid_parameter': is not a member of '`global namespace''
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includeatomic(221,9): error C3861: '_invalid_parameter': identifier not found
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includeatomic(237,9): error C2039: '_invalid_parameter': is not a member of '`global namespace''
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includeatomic(237,9): error C3861: '_invalid_parameter': identifier not found
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includeatomic(297,9): error C2039: '_invalid_parameter': is not a member of '`global namespace''
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includeatomic(297,9): error C3861: '_invalid_parameter': identifier not found
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexpolymorphic_allocator.h(146,13): error C2039: '_invalid_parameter': is not a member of '`global namespace''
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexpolymorphic_allocator.h(146,1): error C3861: '_invalid_parameter': identifier not found
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexpolymorphic_allocator.h(152,13): error C2039: '_invalid_parameter': is not a member of '`global namespace''
5>C:Program FilesMicrosoft Visual Studio2022ProfessionalVCToolsMSVC14.30.30705includexpolymorphic_allocator.h(152,1): error C3861: '_invalid_parameter': identifier not found
Double-clicking on the errors does not bring me to my own source code, but rather to internal Visual C++ source files, so it’s rather hard to guess where things go wrong with my code.
Would anybody have an idea why I get these errors and how to fix them? Things compiled fine with VS2019.