Use a variable on two sources files



  • Hi everyone
    First of all let me apologize about my english :D

    I'm trying to solve an easy problem for most of you...but not for me.
    I create a program where i use Eigen to proceed to matrix calculation.
    From a matrix i extract a value of cell as a double value. It's my matrices.cpp file.
    It works and i can see result with cout command.

    The problem is that i want to extract this value to use in an other cpp. file name traitement.cpp
    I test many command ( static, extern, etc....)

    The problem is that i read so many things, that now i'm lost i dont know where to declare...
    Do i declare it in matrices.h or not ? how do i declare in traitement.h etc...
    below my matrices.cpp extract, and i do not declare in matrices.h

    My traitement.cpp get include instructions #matrices.h
    I tried both, declare or not in matrices.h but it's not defined in traitement.cpp

           MatrixXd Essai {1,1};
                         Essai (0,0) = Levid3(2,2);
                         static double rapeff;
                         rapeff = Essai (0,0);
                         cout << "efficacite : " << rapeff << "\n" ;
    


  • Hello.
    Maybe it's worth trying to figure it out with a simpler example? Create a file matrix_handler.h where the structure of some MatrixHandler class will be described, which, for example, will be able to output numbers to the console. In this file, you list what fields and methods it has. You write the implementation of these methods in the matrix_handler.cpp file, at the beginning of which it says #include matrix_handler.h

    this is .h file

    
    class MatrixHandler 
    {
    public:
    
    	MatrixHandler(int i);
    
    	void do_something_with_stored_int();
    
    	int some_int_to_store;
    
    };
    

    this is .cpp file

    #include "matrix_handler.h"
    
    MatrixHandler::MatrixHandler(int i) 
    {
    	some_int_to_store = i;
    };
    
    void MatrixHandler::do_something_with_stored_int()
    {
    	std::cout << some_int_to_store;
    };
    

    Then in your main file, you'll write #include matrix_handler.h so that main knows that such a class exists and instantiates the MatrixHandler class.
    Having understood how it works, you will already start working with the Eigen library

    Maybe this will give you something


  • Lifetime Qt Champion

    @Benver The question is: how do you use these two classes? Where do you create the instances of these classes?
    You should avoid static and global variables.
    Instead your matrices class should have a getter method which returns the extracted number (store that number as member variable):

    class Matrices
    {
    public:
        double getNumber() { return number; }
    
    private:
        double number;
    }
    

    Other classes then call getNumber() to get that number. Of course the other classes need access to Matrices instance.
    In Qt you can also use signals/slots to exchange data between classes.


Log in to reply