Search code examples
c++classconstructordefault-constructor

got C2758 error for my third party call


I declared my class as follows in my "first.h" :

class MyClass
{
public:
    MyClass ( cv::Mat& _model ) : tmpM ( _model )
    {
    };
private:
cv::Mat& tmpM;
}

then in "first.cpp", I used it like this:

MyClass::tmpM ( cv::Mat& _model )
{
    ...
}

Then I included this "first.h" in my "second.h", and include this "second.h" in my "third.h", then in my "third.cpp" I tried to do this:

cv::Mat testMat ( height, width, CV_8UC3 );
tmpM myM ( testMat );

Then I got errors saying:

'MyClass::tmpM': not a function

error C2512: 'MyClass': no appropriate default constructor available

IntelliSense: declaration is incompatible with "cv::Mat& MyClass::tmpM"

IntelliSense: explicit type is missing ('int' assumed)

Before you ask, I have to say I indeed searched and asked about this, but still couldn't solve it. I am a learner, so could someone help me out of this? thank you.


Solution

  • The errors you got are quite explicit.

    'MyClass::tmpM': not a function
    

    Means that tmpM is not a function and it comes from the declaration

    MyClass::tmpM ( cv::Mat& _model ) {
        ...
    }
    

    I think you are confused and wanted to declare the constructor, in which case you should have written in your header file (i added a default constructor)

    class MyClass {
    public:
        MyClass ( cv::Mat& _model );
    private:
        MyClass(); // Private default constructor
        cv::Mat& tmpM;
    }
    

    And then in your cpp file

    MyClass::MyClass( cv::Mat& _model ) {
        // Do things
    }
    

    This will solve your second error message:

    error C2512: 'MyClass': no appropriate default constructor available
    

    Your third file is also problematic because in line

    tmpM myM ( testMat );
    

    tmpM is not a type. I think you meant

    MyClass myM( testMat );
    

    and that's what the two last errors are telling you.