Skip to main content
gamedev.net
🔒 Locked

+ - * /

Started by Allebarde Apr 16, 2002 at 4:27 AM 4 replies 300+ views
Original Post
Allebarde
Allebarde
Hi! I have a problem: VECTOR3D Vector2 = vB - vA; I would like this line to do this: Vector2.x =vB.x - vA.x; Vector2.y =vB.y - vA.y; Vector2.z =vB.z - vA.z; I saw in a code that a guy (DigiBen) used this: struct VECTOR3D { public: // A default constructor VECTOR3D() {} VECTOR3D(float X, float Y, float Z) { x = X; y = Y; z = Z; } // Here we overload the + operator so we can add vectors together VECTOR3D operator+(VECTOR3D vVector) { // Return the added vectors result. return VECTOR3D(vVector.x + x, vVector.y + y, vVector.z + z); } // Here we overload the - operator so we can subtract vectors VECTOR3D operator-(VECTOR3D vVector) { // Return the subtracted vectors result return VECTOR3D(x - vVector.x, y - vVector.y, z - vVector.z); } (....) float x, y, z; } Shall I always add a constructor?? If i do this in my code, i get a lot of errors from VC++. It asks for a constructor for each structure.. Can someone explain me?? Thanks a lot
SabreMan
SabreMan
What sort of errors are you getting? Bear in mind that if you add your own constructor, you then disable the default ctor that the compiler adds for you. If you need the default ctor in these circumstances, then write one.

Also, this code:

VECTOR3D(float X, float Y, float Z)
{
x = X; y = Y; z = Z;
}

would be better written as:

VECTOR3D(float X, float Y, float Z)
: x(X), y(Y), z(Z)
{}

Additionally, your overloaded operators should take their parameters by const-ref rather than value.
Allebarde
Allebarde
I get those errors:

with this line: VECTOR3D g_vTriangle[3];
PARTICLES particle[MAX_PARTICLES];
err: ''VECTOR3D'' : no appropriate default constructor available

VECTOR3D result={0.0f, 0.0f, 0.0f};
err: ''result'' : non-aggregates cannot be initialized with initializer list

I have 32 errors when i put in my code:

VECTOR3D(float X, float Y, float Z)
: x(X), y(Y), z(Z)
{}

Shall i need an empty constructor for each structure??
SabreMan
SabreMan
Yes, you need a default ctor of the form:

VECTOR3D()
: x(0), y(0), z(0)
{
}
Allebarde
Allebarde
The problem is that:

PARTICLES particle[MAX_PARTICLES]; i get: ''part'' : no appropriate default constructor available
Shall I add a constructor to each structure??

If i have, This is shit! Cause i don''t want to add 100 000 constructors to my 100 000 structures!
SabreMan
SabreMan
I've already answered this. Yes - arrays require a default ctor. You might like to try experimenting with std::vector, which will allows you to perform an initial reserve() for your 100,000 elements, and then will perform in-place construction when you actually add the elements, so that you only get the performance hit once.

C++ FAQ Lite | ACCU | Boost | Learning C++

[edited by - SabreMan on April 16, 2002 6:23:01 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.