includes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madshov
    New Member
    • Feb 2008
    • 20

    includes

    Hi,

    How do I properly include classes that are dependable of each other, without getting an error that the compiler don't knoe anything about one of the classes?
    Ex:

    vector.h
    Code:
    class Vector
    {
     	public:
    ...
    	Vector operator*(const Matrix& m) const;
    }
    matrix.h
    Code:
    class Matrix
    {
    ...
           Vector operator* (const Vector& v) const;
    }
    thanks
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    Don't include vector.h in matrix.c/matrix.h because then you will get interdependency errors.... Instead, if you don't call methods/members of vector in matrix, you only need to tell the compiler in matrix.h that there exists a class class vector, without fleshing out the details (and vice-versa for vector.h):
    [CODE=cpp]
    //vector.h:
    class Matrix; // declaration only: Matrix is a class
    class Vector
    {
    public:
    Vector operator*(const Matrix& m) const;
    }

    //matrix.h:
    class Vector; // declaration only: Vector is a class
    class Matrix
    {
    ...
    Vector operator* (const Vector& v) const;
    }
    [/CODE]

    Comment

    • madshov
      New Member
      • Feb 2008
      • 20

      #3
      Yes of course! Now I see. Thanks for clarifying.

      Comment

      Working...