Sign in to follow this  
webwraith

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628375
    • Total Posts
      2982319
  • Similar Content

    • By sveta_itseez3D
      itSeez3D, a leading developer of mobile 3d scanning software, announced today a new SDK for its automatic 3D avatar generation technology, Avatar SDK for Unity. The Avatar SDK for Unity is a robust plug-n-play toolset which enables developers and creatives to integrate realistic user-generated 3D avatars into their Unity-based applications. SDK users can allow players to create their own avatars in the application or integrate the SDK into their own production processes for character design and animation.
      “Virtual avatars have recently become increasingly popular, especially in sports games and social VR apps. With the advance of VR and AR, the demand to get humans into the digital world is only increasing”, said Victor Erukhimov, itSeez3D CEO. “Our new Avatar SDK for Unity makes it super-easy to bring the avatar technology into any Unity-based game or VR/AR experience. With the Avatar SDK for Unity now every developer can bring face scanning technology into their games and allow players to create their own personalized in-game avatars, making the gameplay much more exciting and immersive.”
      Key features of the Avatar SDK for Unity:
      Automatic generation of a color 3D face model from a single selfie photo in 5-10 seconds (!). Works best with selfies, but can be used with any portrait photo.
      Shape and texture of the head model are unique for each person, synthesized with a deep learning algorithm crafted by computer vision experts
      Head models support runtime blendshape facial animations (45 different expressions)
      Generated 3D heads include eyes, mouth, and teeth
      Algorithms synthesize 3D meshes in mid-poly resolution, ~12k vertices, and ~24k triangles
      Six predefined hairstyles with hair-recoloring feature (many more available on request)
      Avatar generation API can be used in design-time and in run-time, which means you can allow users to create their own avatars in your game
      Cloud version is cross-platform, and offline version currently works on PCs with 64-bit Windows (support for more platforms is coming soon)
      Well-documented samples showcasing the functionality.
       
      Availability
      The Avatar SDK for Unity is offered in two modes - “Cloud” and “Offline”. The “Cloud” version is available at http://avatarsdk.com/ and the “Offline” version is available by request at support@itseez3d.com.
      ###
      About itSeez3D
      At itSeez3D, we are working on the computer vision technology that turns mobile devices into powerful 3D scanners. itSeez3D has developed the world's first mobile 3D scanning application that allows to create high-resolution photorealistic 3D models of people's' faces, bodies and objects. The application is available for iOS and Windows OS mobile devices powered with 3D cameras. In 2016 the company introduced Avatar SDK that creates a realistic 3D model of a face from a single selfie photo. To learn more about itSeez3D scanning software and 3D avatar creation technology, please visit www.itseez3d.com and www.avatarsdk.com.

      View full story
  • Popular Now