Link to home
Start Free TrialLog in
Avatar of Wanderinglazyeye
Wanderinglazyeye

asked on

pointer code not right?

I am compiling fine but getting an exception at this point in my code:

CvMat *theMatrix = FxLUTInst.getMat();
        int n_rows = theMatrix->rows;             //here

FxLUTInst getMat() is an instance of class FxLUT and should return the matrix lut_mat. I am trying to access lut_mat array elements in a matrix created elsewhere in the program, but I think I have my pointer language wrong. I think this is probably simple...

Thanks
WLE.
SOLUTION
Avatar of HuyBD
HuyBD
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Infinity08
It seems like your getMat() returns a NULL pointer.

From this other thread of yours :

https://www.experts-exchange.com/questions/22730694/linker-error-with-externed-value.html

Did you remove this line from the WriteLUT function ? :

        CvMat* lut_mat;

???

If that's not the problem, then can you show the code for the FxLUTInst class and its methods ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wanderinglazyeye
Wanderinglazyeye

ASKER

Hi all,

-It is a runtime exception.
-I had removed the line CvMat* lut_mat; from the WriteLUT function. The matrix was being written to correctly in that part.
-Infinity08 may correct about passing NULL pointer

---------
LUT.h
--------
class FxLUT
{

private :
   CvMat *lut_mat;



public:
    FxLUT();
    ~FxLUT();

    bool WriteLUT();
    CvMat *getMat() { return lut_mat; }

};
---------
LUT.cpp (this works fine, checked the matrix values with Infinity08's help, thread above)
----------
FxLUT::FxLUT()
{
}

FxLUT::~FxLUT()
{
}

//function to create lookup table

bool FxLUT::WriteLUT()
{
CvMat* lut_mat    = cvCreateMat(1,256,CV_8UC1);
int m_cols          = lut_mat->cols;
int m_rows          = lut_mat->rows;
int *data           = lut_mat->data.i;

            for (int i = 0; i < m_rows; ++i)
        {
            for (int j = 0; j < m_cols; ++j)
                {
                    if ((j>105)&&(j<117))   //fill ranges
                        {
                            cvSetReal2D(lut_mat, i, j, j); // Set M(i,j)
                        }
                    else
                        {
                            cvSetReal2D(lut_mat, i, j, 255); // Set M(i,j)
                        }
                }
        }

        int n_rows = lut_mat->rows;
        int n_cols = lut_mat->cols;
        int *data2 = lut_mat->data.i;
---------------
Thread.cpp:
---------------
extern FxLUT FxLUTInst;

CvMat *theMatrix = FxLUTInst.getMat();
        // use the matrix ...
        n_rows = theMatrix->rows; //RUN-TIME EXCEPTION this line

 DeepuAbrahamK's code generated this error:
198 C:\...\OpenCVThread1.cpp [Error C2440] '=' : cannot convert from 'FxLUT *' to 'CvMat *'

Thanks everybody -
WLE
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, I call it before starting the thread. The code changes you suggested fixed the problem. I ran a check on matrix values from the thread, and the values are fine. We know what the error was, but can you briefly explain (if not too onerous an explanation) what the problem was?

Many thanks,

WLE
>> We know what the error was, but can you briefly explain (if not too onerous an explanation) what the problem was?

Ok, if you havea class that has a
Let me try again, without clicking submit too early ... :)

>> We know what the error was, but can you briefly explain (if not too onerous an explanation) what the problem was?

Objects have data members. Those reflect the current state of the object. In your case, there is one data member called lut_mat that contains the matrix :

        class FxLUT {
            private :
                CvMat *lut_mat;
            // etc.
        };

If you create an instance of the FxLUT class, then it will also automatically contain a lut_mat matrix.

When you want to fill the matrix, you need to fill the member matrix. What you did, was create a local matrix (local to the function) in the WriteLUT function. In other words, that matrix was lost when the WriteLUT function ended.
With the change I suggested, you're filling up the member matrix, which will stay for the lifetime of the object.

For more information on classes, check this tutorial :

        http://www.cplusplus.com/doc/tutorial/classes.html
        http://www.cplusplus.com/doc/tutorial/classes2.html

It's an interesting read, and will make things a lot clearer for you !


Btw, you probably should remove the matrix in the class destructor ...