The files are:

main.cc

//g++ main.cc -o main `pkg-config gtkmm-3.0 --cflags --libs` -lstdc++fs #include "mainwindow.cc" #include "catalog.cc" #include "mainwindow.h" #include <gtkmm/application.h> int main(int argc, char *argv[]) { auto app = Gtk::Application::create("org.gtkmm.example"); MainWindow window; return app->run(window, argc, argv); } 

mainwindow.h

 #ifndef GTKMM_MAINWINDOW_H #define GTKMM_MAINWINDOW_H #include <gtkmm.h> #include "catalog.h" class MainWindow : public Gtk::Window { public: MainWindow(); virtual ~MainWindow(); protected: void add_catalog(); //Child widgets: Gtk::Box m_mainBox; Gtk::Grid m_grid; Catalog *m_Catalog0 = new Catalog("/var/www/cscart.site"); Catalog *m_Catalog1 = new Catalog("/var/www"); Catalog *m_Catalog2 = new Catalog("/var/www/cscart.site"); Catalog *m_Catalog3 = new Catalog("/var/www"); Gtk::ButtonBox m_bottom_buttons_box; Gtk::Button m_button_add; }; #endif //GTKMM_MAINWINDOW_H 

mainwindow.cc

 #include "mainwindow.h" MainWindow::MainWindow() : m_mainBox(Gtk::ORIENTATION_VERTICAL), m_button_add("+") { set_title("FM 4x4"); set_border_width(5); set_default_size(500, 500); add(m_mainBox); m_mainBox.pack_start(m_grid); m_grid.attach(*m_Catalog0, 0, 0, 1, 1); m_grid.attach(*m_Catalog1, 1, 0, 1, 1); m_grid.attach(*m_Catalog2, 1, 1, 1, 1); m_grid.remove(*m_Catalog0); m_grid.attach(*m_Catalog0, 0, 0, 1, 2); m_mainBox.pack_start(m_bottom_buttons_box, Gtk::PACK_SHRINK); m_bottom_buttons_box.pack_start(m_button_add, Gtk::PACK_SHRINK); m_bottom_buttons_box.set_border_width(5); m_bottom_buttons_box.set_layout(Gtk::BUTTONBOX_END); m_button_add.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::add_catalog) ); show_all_children(); } MainWindow::~MainWindow() { } void MainWindow::add_catalog() { //m_leftBox.pack_start(*m_Catalog3); m_grid.remove(*m_Catalog0); show_all_children(); } 

catalog.h

 #ifndef GTKMM_MAIN_CATALOG_H #define GTKMM_MAIN_CATALOG_H #include <gtkmm.h> class Catalog: public Gtk::ScrolledWindow { public: Catalog(std::string p); virtual ~Catalog(); void set_path(std::string p); class ModelColumns : public Gtk::TreeModel::ColumnRecord { public: ModelColumns() { add(m_col_name); add(m_col_extension_or_dir_or_file); } Gtk::TreeModelColumn<std::string> m_col_name; Gtk::TreeModelColumn<std::string> m_col_extension_or_dir_or_file; }; ModelColumns m_Columns; Gtk::TreeView m_TreeView; protected: Glib::RefPtr<Gtk::ListStore> m_refListStore; //The Tree Model. }; #endif //GTKMM_EXAMPLE_MESSAGESLIST_H 

catalog.cc

 #include "catalog.h" #include <experimental/filesystem> Catalog::Catalog(std::string p) { set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); set_hexpand(true); set_vexpand(true); /* Обязательно поправить это недоразумение */ set_border_width(1); add(m_TreeView); m_refListStore = Gtk::ListStore::create(m_Columns); m_TreeView.set_model(m_refListStore); Gtk::TreeModel::Row row; namespace fs = std::experimental::filesystem; std::string path = p; for (auto & p : fs::directory_iterator(path)) { row = *(m_refListStore->append()); row[m_Columns.m_col_name] = fs::path(p).filename(); row[m_Columns.m_col_extension_or_dir_or_file] = fs::path(p); } m_TreeView.append_column("Name", m_Columns.m_col_name); m_TreeView.append_column("Extension", m_Columns.m_col_extension_or_dir_or_file); } Catalog::~Catalog() { } 

And the question is:

I want these 4 copies:

 Catalog *m_Catalog0 = new Catalog("/var/www/cscart.site"); Catalog *m_Catalog1 = new Catalog("/var/www"); Catalog *m_Catalog2 = new Catalog("/var/www/cscart.site"); Catalog *m_Catalog3 = new Catalog("/var/www"); 

replace with class vector. How can I do it?

construction type:

 std::vector{Catalog^} arr; arr.push_back(new A(2)); arr[0]; 

does not work.

Although the test file with such a construction works fine.

Closed due to the fact that off-topic participants Kromster , 0xdb , Enikeyschik , Kirill Stoianov , iluxa1810 17 December '18 at 6:52 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, 0xdb, Enikeyschik, Kirill Stoianov, iluxa1810
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And what is the actual question? - Mikhail Rebrov Nov.
  • All is written. I want me to be able to declare a <Catalog *> vector, add / delete elements, access vector elements. I want to replace * m_CatalogN with v_catalog [N]. - 6apa6awka Nov.
  • Yes, only the question became possible to read only after editing the user Fat-Zer, who formatted your code, before the same question was hidden under the code and before it had to be flipped for a very long time. You can see the first edition in history to understand what I mean. - Mikhail Rebrov Nov.
  • .... I do not ask you about the visual editor of this site. I just do not know how to make out. - 6apa6awka Nov.
  • std::vector{Catalog^} arr; - this is what language? - VTT

1 answer 1

The definition of a vector in a class is quite usual:

 class MainWindow : public Gtk::Window { public: MainWindow(); //... std::vector<Catalog*> catalogVec; } 

For initialization, the classic solution will be several push_back in the constructor:

 MainWindow::MainWindow() /* : ... */ { catalogArr.push_back(new Catalog("/var/www/cscart.site")); catalogArr.push_back(new Catalog("/var/www")); // ... } 

In the new versions of C ++ there are also options:

  • Initialization when declaring:

     class MainWindow : public Gtk::Window { //... std::vector<Catalog*> catalogVec = { new Catalog("/var/www/cscart.site"), new Catalog("/var/www"} }; } 
  • Initialization in the constructor initialization list:

     MainWindow::MainWindow() : // ... catalogVec ({new Catalog("/var/www/cscart.site"), new Catalog("/var/www")}); { // ... } 

But with this approach, the responsibility for pointers lies with the programmer. And these objects need to be explicitly destroyed in the destructor (unless of course gtkmm itself cares about it like Qt ; TODO: consult the documentation).

 MainWindow::~MainWindow() { for (auto cat: catalogveg) { delete cat; } } 

Instead, I would recommend using std :: unique_ptr .

  • I can not refer to the element of the vector. It does not work: m_grid.attach (catalogVec [0], 1, 0, 1, 1); - 6apa6awka
  • Since I can't create a vector .... Maybe I can define an instance name with some string? Something like this: for (int i = 0; i <= 3; i ++) {Catalog * string_to_name ('name'.i) = new Catalog (path [i]);} - 6apa6awka
  • I don’t really know the specifics of gtkmm, but judging by the code above, Gtk::Grid::attach accepts an object by value or by reference. so there should be m_grid..attach(*catalogVec[0], 1, 0, 1, 1); - forgot to dereference the pointer ... - Fat-Zer
  • You're right. Thank you very much. And I still thought that since I added an asterisk to the angular brackets, then all the elements are already pointers. - 6apa6awka
  • one
    @ 6apa6awka, if the question is closed, then you should answer the answer "correct", so that it does not hang out ... - Fat-Zer