Thread: Quaternion class problem

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    Quaternion class problem

    I am starting a Quaternion class, and this is what I have so far:
    Code:
    class Vector3D;
    class Matrix3X3;
    class EulerAngle;
    
    class Quaternion {
     public:
      float x, y, z, w;
    
      Quaternion(float a, float b, float c, float d) : x(a), y(b), z(c), w(d) {}
    /*
      Quaternion(float d, const Vector3D &v) : x(v.x), y(v.y), z(v.z), w(d) {}
    */
      Quaternion(const Quaternion &q) : x(q.x), y(q.y), z(q.z), w(q.w) {}
    
     ~Quaternion() {}
    };
    EVERY file is included. When you uncomment the second constructor, i get 3 errors (edited a bit):
    1. x is not a member of Vector3D, as the type is not yet defined in the second contructor.
    2. y is not a member of Vector3D, as the type is not yet defined in the second contructor.
    3. z is not a member of Vector3D, as the type is not yet defined in the second contructor.

    x, y, and z are all public. Any ideas on the problem? Thanks .
    Do not make direct eye contact with me.

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Instead of giving a forward declaration of the class Vector3D, define the class Vector3D before the definition of the class Quaternion..

  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    They are in different files, and it isnt a problem in other files.
    Do not make direct eye contact with me.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Limitations of forward declaration:
    - You can only use it if the size of the forwarded declaration is not needed
    - You can not access any elements of the forwarded type

    Change class Vector3D; to #include "Vector3D.h" (if you know what I mean.)

    gg

  5. #5
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I HAVE included other files, and in other classes I do the same thing, with no problems.
    Do not make direct eye contact with me.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    1. x is not a member of Vector3D, as the type is not yet defined in the second contructor.
    2. y is not a member of Vector3D, as the type is not yet defined in the second contructor.
    3. z is not a member of Vector3D, as the type is not yet defined in the second contructor.
    Are you calling your compiler a liar?

    Vector3D isn't a template class is it?

    Post or attach the header file that contains Quaternion class.

    gg

  7. #7
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    This is the complete header, the only compilation problem is the one I was talking about:
    Code:
    #ifndef __QUATERNION_H_INCLUDED__
    #define __QUATERNION_H_INCLUDED__
    #include <cmath>
    #include "matrix.h"
    #include "vector.h"
    #include "euler.h"
    using namespace std;
    
    class Vector3D;
    class Matrix3X3;
    class EulerAngle;
    
    class Quaternion {
     public:
      float x, y, z, w;
    
      Quaternion(float a, float b, float c, float d) : x(a), y(b), z(c), w(d) {}
    
      Quaternion(float d, const Vector3D &v) : x(v.x), y(v.y), z(v.z), w(d) {}
    
      Quaternion(const Quaternion &q) : x(q.x), y(q.y), z(q.z), w(q.w) {}
    
      ~Quaternion() {}
    
      const Quaternion operator*(float m) const;
      const Quaternion operator/(float m) const;
      Quaternion &operator*=(float m);
      Quaternion &operator/=(float m);
      bool operator==(const Quaternion &q) const;
      bool operator!=(const Quaternion &q) const;
    
      float length() const;
      Quaternion normalize() const;
      Quaternion conjugate() const;
    };
    
    #endif
    And no, none of my classes are templates.
    Last edited by Lurker; 11-18-2003 at 05:23 PM.
    Do not make direct eye contact with me.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Try removing the forward declarations. If that doesn't work, post vector.h

    gg

  9. #9
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I removed them, and got these errors:
    Do not make direct eye contact with me.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It looks like it doesn't know what a Vector3D is. Is that declared in vector.h?

    gg

  11. #11
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Is what declared in vector.h? Vector3D? Yes, most definetly!
    Do not make direct eye contact with me.

  12. #12
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Vector.h:
    Code:
    #ifndef __VECTOR3D_H_INCLUDED__
    #define __VECTOR3D_H_INCLUDED__
    #include <cmath>
    #include "matrix.h"
    #include "euler.h"
    #include "quaternion.h"
    using namespace std;
    
    class Matrix3X3;
    class Quaternion;
    class EulerAngle;
    
    class Vector3D {
     public:
      float x, y, z;
    
    /*Constructors / Deconstructors:
     Vector3D v: x, y, and z are all undefined.
     Vector3D v2(1, 2, 3): x, y, and z are set to 1, 2, and 3, respectivly.
     Vector3D v3(Vector3D v4): v3.x, v3.y, and v3.z are all set to v4.x, v4.y, and v4.z, respectivly.
     ~Vector3D(): Deconstructs the vector.
    */
      Vector3D() {}
    
      Vector3D(float a, float b, float c) : x(a), y(b), z(c) {}
    
      Vector3D(const Vector3D &v) : x(v.x), y(v.y), z(v.z) {}
    
      ~Vector3D() {}
    
    /*Operator overloadings:
     Vector3D operator+(const Vector3D &v): x, y, and z are added to v.x, v.y, and v.z, respectivly.
     Vector3D operator-(const Vector3D &v): x, y, and z are subtracted from v.x, v.y, and v.z, respectivly.
     Vector3D operator*(float w): x, y, and z are all multiplied by w.
     Vector3D operator/(float w): x, y, and z are all divided by w.
     Vector3D operator-(): negate the vector - x, y, and z are set to -x, -y, and -z, respectivly
     Vector3D &operator+=(const Vector3D &v): x, y, and z are added to v.x, v.y, and v.z, respectivly, and vector is set with new values.
     Vector3D &operator-=(const Vector3D &v): x, y, and z are subtracted from v.x, v.y, and v.z, respectivly, and vector is set with new values.
     Vector3D &operator*=(float w): x, y, and z are all multiplied by w, and vector is set with new values.
     Vector3D &operator/=(float w): x, y, and z are all divided by w, and vector is set with new values.
     Vector3D &operator=(const Vector3D &v): this vector is set to v.
     bool operator==(const Vector3D &v): returns true if Vector3Ds' are equal, false otherwise.
     bool operator!=(const Vector3D &v): returns true if Vector3Ds' are not equal, false otherwise.
    */
      const Vector3D operator+(const Vector3D &v) const;
      const Vector3D operator-(const Vector3D &v) const;
      const Vector3D operator*(float w) const;
      const Vector3D operator/(float w) const;
      Vector3D operator-() const;
      Vector3D &operator+=(const Vector3D &v);
      Vector3D &operator-=(const Vector3D &v);
      Vector3D &operator*=(float w);
      Vector3D &operator/=(float w);
      Vector3D &operator=(const Vector3D &v);
      bool operator==(const Vector3D &v) const;
      bool operator!=(const Vector3D &v) const;
    
    /*Other functions:
     float length(): returns the length of the vector.
     Vector3D normalize(): normalize the vector, vector / length of vector
     float dotProduct(const Vector3D &v): returns the dotProduct, x * v.x + y * v.y + z * v.z
     Vector3D crossProduct(const Vector3D &v): returns cross product between this vector and v.
     float distance(const Vector3D &v): returns distance between this vector and v.
     float angleBetween(const Vector3D &v): returns the angle (in radians) between the 2 vectors.
    */
      float length() const;
      Vector3D normalize() const;
      float dotProduct(const Vector3D &v) const;
      Vector3D crossProduct(const Vector3D &v) const;
      float distance(const Vector3D &v) const;
      float angleBetween(const Vector3D &v) const;
    };
    
    #endif
    Do not make direct eye contact with me.

  13. #13
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    This is euler.h, and it compiles without error:
    Code:
    #ifndef __EULER_H_INCLUDED__
    #define __EULER_H_INCLUDED__
    #include <cmath>
    #include "matrix.h"
    #include "vector.h"
    #include "quaternion.h"
    using namespace std;
    
    class Vector3D;
    class Matrix3X3;
    class Quaternion;
    
    class EulerAngle {
     public:
      float h, p, b;
    
    /*Constructors / Deconstructors:
     EulerAngle e: all variables are undefined.
     EulerAngle e2(1, 2, 3): heading is set to 1, pitch is set to 2, bank is set to 3.
     EulerAngle e3(Vector3D v): h is set to v.x, p is set to v.y, and b is set to v.z.
     EulerAngle e4(EulerAngle e): h, p, and b are set to e.h, e.p, and e.b, respectivly.
    */
      EulerAngle(float a, float b, float c) : h(a), p(b), b(c) {}
    
      EulerAngle(const Vector3D &v) : h(v.x), p(v.y), b(v.z) {}
    
      EulerAngle(const EulerAngle &ea) : h(ea.h), p(ea.p), b(ea.b) {}
    
      ~EulerAngle() {}
    };
    
    #endif
    Do not make direct eye contact with me.

  14. #14
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Ahh, ok.

    Only #inlude what you need. If you don't use Quaternion class, then don't include it.

    The problem is circular inclusion is preventing the compiler from knowing about Vector3D. Here is an example:

    Code:
    #ifndef _A_H_
    #define _A_H_
    
    #include "B.h"
    
    struct A
    {
        int n;
    };
    
    #endif
    Code:
    #ifndef _B_H_
    #define _B_H_
    
    #include "A.h"
    
    struct B
    {
        int n;
    
        B(A a) : n(a.n) {}
    };
    
    #endif
    Code:
    #include "A.h"
    #include "B.h"
    
    int main()
    {
        A a;
        a.n = 3;
    
        B b(a);
    
    	return 0;
    }//main
    B.h(10) : error C2629: unexpected 'struct B ('
    B.h(10) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
    main.cpp(11) : error C2440: 'initializing' : cannot convert from 'struct A' to 'struct B' - No constructor could take the source type, or constructor overload resolution was ambiguous

    My errors are a little different but it's the same problem.
    Since A doesn't need to know anything about B, A.h shouldn't include B.h

    Go through your headers and clean em up.

    gg

  15. #15
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Ahhhhhh, yay . Thanks, I'm EXTREMELY grateful .
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM