Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ class question

Options
  • 20-07-2010 12:01am
    #1
    Closed Accounts Posts: 14


    Hey, I'm somewhat new to C++ and just wondering if there's a way to do the following. I've got two classes and want to be able to pass each one as an argument to another, eg:
    matrix3.multiply(vector3);
    
    vector3.multiply(matrix3);
    
    I've hit an obvious problem in declaring these classes - each one is expecting the other to be defined. Is there any way around this?

    Any advice appreciated!


Comments

  • Registered Users Posts: 2,150 ✭✭✭dazberry


    I'm not a c++ developer but lookup the concept of forward class declarations, something like this:
    class matrix3;
    
    class vector3
    {
      public void multiply (matrix3 m3)
      {
      // stuff
      }
    };
    
    class matrix3
    {
      public void multiply (vector3 v3)
      {
      // stuff
      }
    };
    
    

    HTH

    D.


  • Closed Accounts Posts: 14 Toe Twister


    Thanks, dazberry - that's done the trick!

    Code looks something like:

    vector3.h:
    class matrix3;  // Forward declaration.
    
    class vector3
    {
      // stuff
      vector3 multiply(const matrix3&);
    };
    
    vector3.cpp:
    #include "vector3.h"
    #include "matrix3.h"
    
    vector3 vector3::multiply(const matrix3& m) 
    {
      // stuff
    }
    


Advertisement