Jump to content
  • Advertisement
Sign in to follow this  
webwraith

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

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

If you intended to correct an error in the post then please contact us.

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);


        /*standard addition operators
          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"

/***********************************
 * mglVector3 overloaded operators *
 *          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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!