简体   繁体   中英

C++ Use object from main.cpp's file

I have a program (matrix.hpp, matrix.cpp, main.cpp) and the class Matrix_t. In main.cpp i create a object 'A' with the constructor's class, and i have a function in matrix.cpp:

it_t Matrix_t::element(ix_t i,ix_t j,Matrix_t& SM){
   if ((i<1)||(i>m_)||(j<1)||(j>n_)){
  cerr << "Error" << endl;
  return 0;    }
   int pos = (i-1)*SM.n_+j-1;
   return SM.base_[pos]; }

If i create a new object 'B' but in matrix.cpp, i can call the previous function of this way: element(1,1,B). But if i pass the object 'A' created in main.cpp? If i code: element(1,1,A) i get a error (A didn't declared, logic).

What's the form to call element() with 'A' object?

PD: this is the main.cpp

 int main(int argc,char** argv) 
 {   Matrix_t A;

 A.read(cin);   
     A.write(cout);

 const double det=A.determinant();  
     cout << "Determinant: " << det <<
                        "\n" << endl;

    return 0; 
  }

You can call Matrix_t::element on A within the scope, where A is visible. So in the main function

 #include <iostream>
 #include "matrix.hpp"

 using namespace std;

 int main(int argc,char** argv) 
 {
     Matrix_t A;

     A.read(cin);   
     A.write(cout);

     const double det=A.determinant();  
     cout << "Determinant: " << det << "\n" << endl;


    //-----------
    auto z = A.element(1,1,B);
    // or if you wish, B.element(1,1,A)
    //-----------

    return 0; 
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM