需要调用gtk_init函数,但不能从main函数调用 - C ++

时间:2017-06-08 02:37:46

标签: c++ gtk gtkmm

我正在使用GTK3来创造一些东西。在教程中有

#include <gtk/gtk.h>
int main(int argc, char* argv[])
{
    GtkWidget *window;
    gtk_init(&argc, &argv);

gtk_window, gtk_container etc...

我想创建MainProgram,它将处理此main函数之外的图形。所以我删除了上面的代码并创建了一个新代码:

的main.cpp

#include "MainProgram.h"
int main(int argc, char* argv[])
{
    MainProgram mainProgram;
    mainProgram.run(); // the magic is now happening inside this run method

MainProgram.h

#include <gtk/gtk.h>
#include "MainProgram.h"
class MainProgram
{
public:
    MainProgram();
    ~MainProgram();

    void run();

    void initSystems();
...other not so important code goes inside

最后,MainProgram.cpp

#include "MainProgram.h"

MainProgram::MainProgram()
{
...
}

MainProgram::~MainProgram(){}


void MainProgram::run()
{   
// about that magic we were talking about
    initSystems();
}

void MainProgram::initSystems(){

    gtk_init(&argc, &argv); // here it is. 

    _window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_position(GTK_WINDOW(_window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(_window), _screenWidth, _screenHeigth); 
    gtk_window_set_title(GTK_WINDOW(_window), "Fill & stroke");
    // ************** FIX OF THE PROBLEM
    gtk_widget_show_all(_window); // DIDNT CALL
    gtk_main();                   // These two functions
}

现在,当我尝试用以下代码编译程序时:

g++ main.cpp MainProgram.cpp `pkg-config --cflags --libs gtk+-3.0`

我收到以下警告:

MainProgram.cpp: In member function ‘void MainProgram::initSystems()’:
MainProgram.cpp:22:12: error: ‘argc’ was not declared in this scope
  gtk_init(&argc, &argv);
            ^
MainProgram.cpp:22:19: error: ‘argv’ was not declared in this scope
  gtk_init(&argc, &argv);

有没有一种简单的方法可以从main.cpp中的main函数传递这些地址?我这样做是为了让我的代码看起来更干净,并试图不重载main.cpp文件:)

1 个答案:

答案 0 :(得分:0)

我忘了放两个功能来显示我的应用程序,你可以在MainProgram.cpp文件中看到,我做了标记。它的最后几行文件&#39;。