Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use a variable on two sources files
Forum Updated to NodeBB v4.3 + New Features

Use a variable on two sources files

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 300 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Benver
    wrote on last edited by
    #1

    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" ;
    
    jsulmJ 1 Reply Last reply
    0
    • goloviznin.kG Offline
      goloviznin.kG Offline
      goloviznin.k
      wrote on last edited by goloviznin.k
      #2

      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

      1 Reply Last reply
      0
      • B Benver

        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" ;
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved