Hi I am trying to forward declarate the cv::Mat class but I cant get it to work. It gives the message field 'frame' has incomplete type.
OpenGlImpl.h
namespace cv {
class Mat;
}
class OpenGLImpl {
private:
cv::Mat frame;
};
How should I properly forward declarate this?
You cannot use a forward declaration here. The compiler needs to have the definition of cv::Mat in order for it to be a data member of OpenGLImpl.
If you want to avoid this constraint, you could have OpneGLImpl hold a (smart) pointer to cv::Mat:
#include <memory>
namespace cv {
class Mat;
}
class OpenGLImpl {
private:
std::unique_ptr<cv::Mat> frame;
};
You can then instantiate the cv::Mat owned by the unique_ptr in an implementation file.
Note that a reference would also work with a forward declaration, but it is unlikely you need refernce semantics here.
§ 3.9.5
A class that has been declared but not defined, or an array of unknown size or of incomplete element type, is an incompletely-defined object type.43 Incompletely-defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.
struct X; // X is an incomplete type
X* xp; // OK, xp is a pointer to an incomplete type.
struct Y
{
X x; // ill-formed, X is incomplete type
}
struct Z
{
X* xp; // OK, xp is a pointer to an incomplete type
}
void foo() {
// xp++; // ill-formed: X is incomplete
}
struct X { int i; }; // now X is a complete type
X x; // OK, X is complete type, define an object is fine
void bar() {
xp = &x; // OK; type is “pointer to X”
}
void t()
{
xp++; // OK: X is complete
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With