'access to variable in c++ inner class
I'm using cairomm, opencv4, box2d(not revealed in below code) to make physics education video.
My plan is like this. Many Sobject(Scientific obect) constitute a Scene. So I decided to use inner-class.(I don't know it's best design, I'm newbie to programming)
My problem is in below.
cv::Mat Surface2Mat(Cairo::RefPtr<Cairo::ImageSurface> surface)
{
cv::Mat Frame(surface->get_height(), surface->get_width(), CV_8UC4, surface->get_data(), surface->get_stride());
cv::cvtColor(Frame, Frame, cv::ColorConversionCodes::COLOR_BGRA2BGR);
return Frame;
}
class Scene
{
private:
int surface_width = 1920;
int surface_height = 1080;
int second = 4;
cv::Size resolution = {surface_width, surface_height};
const int mp4 = cv::VideoWriter::fourcc('a', 'v', 'c', '1');
cv::VideoWriter video;
double FPS;
public:
class Sobject
{
private:
double width;
double height;
double color[3] = {1, 1, 1};
public:
Cairo::RefPtr<Cairo::Context> cr;
Cairo::RefPtr<Cairo::ImageSurface> surface;
Sobject()
{
surface = Cairo::ImageSurface::create(Cairo::Format::FORMAT_ARGB32, 1920, 1080);
cr = Cairo::Context::create(surface);
}
void
set_position(double x = 0, double y = 0)
{
cr->move_to(960, 540);
}
void drawCircle()
{
cr->arc(0, 0, 40, 0.0, 2 * M_PI);
}
};
Scene(int width = 1920, int height = 1080)
{
video.open("Scene.mp4", mp4, FPS = 30, resolution, true);
}
~Scene()
{
video.release();
}
void write()
{
for (int i = 0; i < (FPS * second); i++)
{
video.write(Surface2Mat(surface));
}
}
};
The "video.write(Surface2Mat(surface));" at last makes problem. How can I access to the surface?
Solution 1:[1]
You have not created an instance of Sobject. You can do something like:
class Scene
{
....
public:
class Sobject
{...};
Sobject mSobject; // can have it public or private depending on other requirements
....
void write()
{
for (int i = 0; i < (FPS * second); i++)
{
// now that you have an instance of Sobject, you can use surface from it
video.write(Surface2Mat(mSobject.surface));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ankit Kumar |
