-3

Why in following code pointer to EVectors and EValueswhether in main function or in MYLDA function become null? and how to fix it?

void main()
    {
      // some code
      Mat EVectors;
      Mat EValues;
      uchar* pEVectors = EVectors.data;
      uchar* pEValues = EValues.data;          
      MYLDA(train_labels, train_data, pEVectors, pEValues);
      // some code
    }

void MYLDA(vector<int> gnd, Mat_<float> _data, uchar* eigvector, uchar* eigvalue)
    {
      // some code
      GeneralizedEigenSolver<MatrixXf> ges;  
      ges.compute(DPrime, WPrime);    
      MatrixXcf eigenvectorsInEigen = ges.eigenvectors();      
      VectorXcf eigenvaluesInEigen = ges.eigenvalues();  
      Mat eigenvectorsOpenCV(eigenvectorsInEigen.rows(), 
      eigenvectorsInEigen.cols(), CV_32FC1, eigenvectorsInEigen.data());
      eigvector = eigenvectorsOpenCV;      
      Mat eigenvaluesOpenCV(eigenvaluesInEigen.rows(), 
      eigenvaluesInEigen.cols(), CV_32FC1, eigenvaluesInEigen.data());
      eigvalue = eigenvaluesOpenCV;   
    }
Long Luong
  • 764
  • 2
  • 14
  • 28
Saeid
  • 508
  • 1
  • 4
  • 20
  • Were they null when you assigned them? Where did they become null? Both of those can be answered by a debugger. – chris Dec 30 '17 at 17:49
  • Assigning to a parameter has no effect outside the function in which you're doing it. – molbdnilo Dec 30 '17 at 17:50
  • @molbdnilo what do you mean? Could you explain in more detail? – Saeid Dec 30 '17 at 17:52
  • @chris they were null both in `main` func and `MYLDA` func. I've mentioned this in my question. – Saeid Dec 30 '17 at 17:56
  • So they're null when you initialize them? Or do they become null later in the function? The first step to figuring out why they become null is to figure out exactly which statement makes them null. Anyway, the question, to me, sounded like they became null in one of the two functions and you didn't know which. – chris Dec 30 '17 at 18:01
  • @Saeid There is nothing special about pointers. Assigning to `eigvalue` or `eigvector` does not modify the variables whose values you passed in. – molbdnilo Dec 30 '17 at 18:18
  • @molbdnilo so `eigvalue` and `eigvector` even in `MYLDA` function are `null`. why? – Saeid Dec 30 '17 at 18:27
  • 1
    Where do you expect the `data` member of those empty `Mat`s to point to? – Dan Mašek Dec 30 '17 at 18:37
  • 1
    Possible duplicate of [Function does not change passed pointer C++](https://stackoverflow.com/questions/11842416/function-does-not-change-passed-pointer-c) – Stephan Lechner Dec 30 '17 at 19:06

1 Answers1

0

One way to solve this problem is:

    void main()
    {
      // some code
      Mat EVectors;
      Mat EValues;

      MYLDA(train_labels, train_data, &EVectors, &EValues);
      // some code
    }

void MYLDA(vector<int> gnd, Mat_<float> _data, Mat* eigvector, MAt* eigvalue)
    {
      // some code
      *eigvector = eigenvectorsOpenCV;      

      *eigvalue = eigenvaluesOpenCV;   
    }
Saeid
  • 508
  • 1
  • 4
  • 20