# Unity Class Chaos (yes, again...:( )

This topic is 4361 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi again![embarrass] hopefully, this should be a quick one. After my last thread (Class Chaos ver. 1) I had a fully working vector class. Now the only thing I would like to know is, how do I change my vector to be turned into a static library? This is the vector code: mglvector.h;
#ifndef MGLVECTOR_H
#define MGLVECTOR_H

/************************************
* mglVector3:- contains x,y, and z *
*              all are floats      *
************************************/

class mglVector3
{
public:
mglVector3():x(0.0f),y(0.0f),z(0.0f){}
mglVector3(float x_,float y_,float z_);
mglVector3(const mglVector3& mVec);
~mglVector3();

// operators- yay :P XO

mglVector3& operator=(const mglVector3 mVec);
mglVector3& operator=(const float array[3]);

mglVector3& operator+=(const mglVector3& mVec);
mglVector3& operator+=(const float array[3]);

mglVector3& operator-=(const mglVector3& mVec);
mglVector3& operator-=(const float array[3]);

mglVector3& operator*=(float var);

the form this vector+array*/
friend mglVector3 operator+(const mglVector3& mVec,float array[3])
{
return mglVector3(array[0] + mVec.x, array[1] + mVec.y, array[2] + mVec.z);
}

//the form array+this vector
friend mglVector3 operator+(float array[3],const mglVector3& mVec)
{
return mglVector3(array[0] + mVec.x, array[1] + mVec.y, array[2] + mVec.z);
}

//the form this vector+other vector
friend mglVector3 operator+(mglVector3 mVec1,const mglVector3& mVec2)
{
return mglVector3(mVec1.x + mVec2.x, mVec1.y + mVec2.y, mVec1.z + mVec2.z);
}

//the form other vector+this vector
friend mglVector3 operator+(const mglVector3& mVec1,mglVector3 mVec2)
{
return mglVector3(mVec1.x + mVec2.x, mVec1.y + mVec2.y, mVec1.z + mVec2.z);
}

/*standard subtraction operators
the form this vector-array*/
friend mglVector3 operator-(const mglVector3& mVec,float array[3])
{
return mglVector3(mVec.x - array[0], mVec.y - array[1], mVec.z - array[2]);
}

//the form array-this vector
friend mglVector3 operator-(float array[3],const mglVector3& mVec)
{
return mglVector3(array[0] - mVec.x, array[1] - mVec.y, array[2] - mVec.z);
}

//the form this vector-other vector
friend mglVector3 operator-(mglVector3 mVec1,const mglVector3& mVec2)
{
return mglVector3(mVec1.x - mVec2.x, mVec1.y - mVec2.y, mVec1.z - mVec2.z);
}

//the form other vector-this vector
friend mglVector3 operator-(const mglVector3& mVec1,mglVector3 mVec2)
{
return mglVector3(mVec1.x - mVec2.x, mVec1.y - mVec2.y, mVec1.z - mVec2.z);
}

//the form this vector*value
friend mglVector3 operator*(float var,const mglVector3& mVec2)
{
return mglVector3(mVec2.x * var, mVec2.y * var, mVec2.z * var);
}

//the form value*this vector
friend mglVector3 operator*(const mglVector3& mVec1,float var)
{
return mglVector3(mVec1.x * var, mVec1.y * var, mVec1.z * var);
}

float x;
float y;
float z;

protected:
private:

};


mglvector.cpp;
#include "mglvector.h"

/***********************************
*          and methods            *
***********************************/

mglVector3::~mglVector3()
{
// dtor
}

mglVector3::mglVector3(float x_,float y_,float z_)
{
x=x_;
y=y_;
z=z_;
}

mglVector3::mglVector3(const mglVector3& mVec)
{
x=mVec.x;
y=mVec.y;
z=mVec.z;
}

mglVector3& mglVector3::operator=(const mglVector3 mVec)
{
x=mVec.x;
y=mVec.y;
z=mVec.z;

return *this;
}

mglVector3& mglVector3::operator=(const float array[3])
{
x=array[0];
y=array[1];
z=array[2];

return *this;
};

mglVector3& mglVector3::operator+=(const mglVector3& mVec)
{
x+=mVec.x;
y+=mVec.y;
z+=mVec.z;

return *this;
};

mglVector3& mglVector3::operator+=(const float array[3])
{
x+=array[0];
y+=array[1];
z+=array[2];

return *this;
}

mglVector3& mglVector3::operator-=(const mglVector3& mVec)
{
x-=mVec.x;
y-=mVec.y;
z-=mVec.z;

return *this;
}

mglVector3& mglVector3::operator-=(const float array[3])
{
x-=array[0];
y-=array[1];
z-=array[2];

return *this;
}

mglVector3& mglVector3::operator*=(float var)
{
x*=var;
y*=var;
z*=var;

return *this;
}


Now, this compiles fine as a library,but when I place library and file in the lib and include files of my compiler, and I try to use the vector, I get three errors;
.objs\main.o:main.cpp:(.text+0x224): undefined reference to mglVector3::~mglVector3()'
.objs\main.o:main.cpp:(.text+0x24e): undefined reference to mglVector3::~mglVector3()'
.objs\main.o:main.cpp:(.text\$_ZN10mglVector3C1Efff[mglVector3::mglVector3(float, float, float)]+0x8): undefined reference to vtable for mglVector3'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)

If it helps, I'm using the latest stable version of Code::Blocks. I was just wondering what it is that I'm doing wrong?

##### Share on other sites
You sure the header hasn't changed since you last build that library? That class shouldn't even have a vtable without any polymorphic functions...

Also, just out of curiosity, why the inconsistency with passing by reference?

mglVector3& operator= (const mglVector3  mVec);mglVector3& operator+=(const mglVector3& mVec);

(Also, you seem to be missing your include guard's closing #endif?)

Edit: You also seem to have some duplication going on here:

        //the form this vector+other vector        friend mglVector3 operator+(mglVector3 mVec1,const mglVector3& mVec2)        {            return mglVector3(mVec1.x + mVec2.x, mVec1.y + mVec2.y, mVec1.z + mVec2.z);        }        //the form other vector+this vector        friend mglVector3 operator+(const mglVector3& mVec1,mglVector3 mVec2)        {            return mglVector3(mVec1.x + mVec2.x, mVec1.y + mVec2.y, mVec1.z + mVec2.z);        }

There's no reason to have these seperate operators - try:

//the form vector+vector
friend mglVector3 operator+(const mglVector3& mVec1,const mglVector3& mVec2)
{
return mglVector3(mVec1.x + mVec2.x, mVec1.y + mVec2.y, mVec1.z + mVec2.z);
}

Also, instead of implementing seperate operators for array additions, you should instead give mglVector3 a constructor that accepts arrays - then:

float array[3];mglVector3 a;mglVector3 b = a + array;`

Will automatically create a temporary mglVector3 from a, and call the above version of operator+.

##### Share on other sites
It did have a virtual destructor at one point, but I deleted it.

And the #endif is in the file, but there were some structures that come after, and it was only the mglVector3 class that I was having trouble with. Sorry for the confusion.

Thanks for the advice, though, I'll change that now.

##### Share on other sites
Just as a side note, I would stay away from "virtual" and "protected" and the concepts involved with them until you get your vector class (and C++ syntax) down pat. Even "friends" might be somewhat misleading. In case you're just learning about polymorphism and inheritance, make them their own separate lessons. (Not too long ago, I took my own advice... 1 thing at a time.)

Don't know if that's the case here, but it helps me to introduce 1 new concept at a time, is all.

##### Share on other sites
Nope, just changed it, still the same thing :P

Discman1028: Yup, that's what the last thread was all about. I got my class working too, the only trouble I'm having is when I try to use the compiled static library

• 10
• 14
• 11
• 10
• 11
• ### Similar Content

• Hello fellow devs!
Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

In Pseudo-Code:
angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
index = (int) (angle / (PI / 5));
PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

I can't get my head around it. Any suggestions? Is the whole approach doomed?

Thank you in advance for any input!

• Hi , I was considering this start up http://adshir.com/, for investment and i would like a little bit of feedback on what the developers community think about the technology.
So far what they have is a demo that runs in real time on a Tablet at over 60FPS, it runs locally on the  integrated GPU of the i7 . They have a 20 000 triangles  dinosaur that looks impressive,  better than anything i saw on a mobile device, with reflections and shadows looking very close to what they would look in the real world. They achieved this thanks to a  new algorithm of a rendering technique called Path tracing/Ray tracing, that  is very demanding and so far it is done mostly for static images.
From what i checked around there is no real option for real time ray tracing (60 FPS on consumer devices). There was imagination technologies that were supposed to release a chip that supports real time ray tracing, but i did not found they had a product in the market or even if the technology is finished as their last demo  i found was with a PC.  The other one is OTOY with their brigade engine that is still not released and if i understand well is more a cloud solution than in hardware solution .
Would there  be a sizable  interest in the developers community in having such a product as a plug-in for existing game engines?  How important  is Ray tracing to the  future of high end real time graphics?

• Good day,

I just wanted to share our casual game that is available for android.

Description: Fight your way from the ravenous plant monster for survival through flips. The rules are simple, drag and release your phone screen. Improve your skills and show it to your friends with the games quirky ranks. Select an array of characters using the orb you acquire throughout the game.