Visual C++ Developer Center >
Visual C++ Forums
>
Visual C++ General
>
Problem of struct default constructor in vc++2005 beta1
Problem of struct default constructor in vc++2005 beta1
3512.1 I made a default constructor for a struct as below. But this caused a compliation error in vc++ 2005:
---------------------------------------------------------------------
Error 1 error C3417: 'Point :: Point' : value types cannot contain user-defined special member functions
---------------------------------------------------------------------
the code was copied from a book of vc++ .net 2003. I got this error in VC++ 2005.any idea?
---------------------------------------------------------------------
#include "stdafx.h"using namespace System;
value struct Point
{
public:
int x, y;
Point(){x=0;y=0;}
};int _tmain()
{
return 0;
}
Answers
- This is a limitation of the CLR, now enforced by VC++ 2005: Value types (structs) cannot have "special member functions". The CLR requires that value types be initialized by setting all members to zero and requires them to be memcpy-able.
In your example, that's exactly what your constructor is doing - just get rid of the constructor since the CLR is doing exactly that initialization for you. If you need a constructor that doesn't simply initialize to zero, you'll need to create a factory function (or property).
value struct Point
{
public:
int x, y;
static Point At(int x_, int y_)
{
Point p; // inits to all 0's
p.x = x_;
p.y = y_;
return p;
}
};
All Replies
wrote in message news:fedcfa42-4dde-47fc-ae43-27b15b6f3d7c@discussions.microsoft.com... 3512.1 I made a default constructor for a struct as below. But this caused a compliation error in vc++ 2005: --------------------------------------------------------------------- Error 1 error C3417: 'Point :: Point' : value types cannot contain user-defined special member functions --------------------------------------------------------------------- the code was copied from a book of vc++ .net 2003. I got this error in VC++ 2005. any idea? --------------------------------------------------------------------- #include "stdafx.h" using namespace System; value struct Point { public: int x, y; Point(){x=0;y=0;} }; int _tmain() { return 0; } Note C++/CLI is not MC++. Value types are not allowed to have parameterless contructors in C++/CLI. What you need is this: Point(int x, int y) {this.x = 0, this.y = 0} but this is pointless as the members are by default initialized to o or nullptr by the compiler generated default constructor. Willy - NNTP User, I don't quite understand what you said. C++/CLI is not Mircorsoft C++?
I am reading a book called 'microsoft visual C++ .net step by step version 2003'. The program was copied from that book. parameterless constructor of value type is supposed to work in vc++2003, if what in the book is correct.
is it a new feature of vc++2005?
thx - This is a limitation of the CLR, now enforced by VC++ 2005: Value types (structs) cannot have "special member functions". The CLR requires that value types be initialized by setting all members to zero and requires them to be memcpy-able.
In your example, that's exactly what your constructor is doing - just get rid of the constructor since the CLR is doing exactly that initialization for you. If you need a constructor that doesn't simply initialize to zero, you'll need to create a factory function (or property).
value struct Point
{
public:
int x, y;
static Point At(int x_, int y_)
{
Point p; // inits to all 0's
p.x = x_;
p.y = y_;
return p;
}
}; Riekey wrote: NNTP User, I don't quite understand what you said. C++/CLI is not Mircorsoft C++?
C++/CLI is not MC++ meaning Managed Extensions for C++. VC 2005 essentially uspports three different dialects of C++:
1. ISO 14882 standard C++ (also known as native C++)
2. Managed Extensions for C++ (introduced with VC7/2002). This is the original syntax for interacting with .NET from C++.
3. C++/CLI (introduced with VC8/2005). This is the new syntax for .NET in C++.Riekey wrote:
I am reading a book called 'microsoft visual C++ .net step by step version 2003'. The program was copied from that book. parameterless constructor of value type is supposed to work in vc++2003, if what in the book is correct.
The example may be assuming that you're writing native (ISO standard) C++, not managed C++.
You control which dialect you're compiling using the /clr command line argument (or the related options in the IDE).
/clr not included - native code
/clr : oldSyntax - Managed Extensions for C++
/clr - C++/CLI (may contain a mixture of managed and native code)
/clr : pure - C++/CLI (pure managed code, no embedded native code)
/clr : safe - C++/CLI (verifiable only)
(remove spaces between /clr : option - added to prevent emoticon recognition)


