2

我正在编写一个 GTKmm 窗口程序;主窗口创建了两个按钮,一个是英文的,一个是中文的。用户可以单击按钮以适当的语言显示不同的窗口。目前我无法在主窗口内初始化多项目容器。它是一个 MainWindowPane 类型的对象,它继承了 Gtk::HBox。

当我尝试制作时,编译器会发出以下错误:

$ make 
g++ -g `pkg-config gtkmm-2.4 --cflags` -c MainWindow.cpp  
g++  -g -o QPI_frontend main.o MainWindow.o StartButton.o `pkg-config gtkmm-2.4 --libs`  
MainWindow.o: In function `MainWindow':  
/home/dmurvihill/Documents/QPI_frontend/MainWindow.cpp:9: undefined reference to   `MainWindowPane::MainWindowPane()'  
/home/dmurvihill/Documents/QPI_frontend/MainWindow.cpp:9: undefined reference to  `MainWindowPane::MainWindowPane()'  
collect2: ld returned 1 exit status  
make: *** [QPI_frontend] Error 1  

我正在使用最新版本的 gcc 和 pkg-config 来包含正确的库。我也是一个java人。

/*
 * MAIN_WINDOW.H
 * Responsible for creating "slave" RSED windows. Can create English or Chinese
 * versions of the demo, and can destroy them all with one click.
 */  
#ifndef MAINWINDOW_H  
#define MAINWINDOW_H  

#include <gtkmm/window.h>

//#include "SlaveWindow.h"
#include "StartButton.h"
#include "MainWindowPane.h"

class MainWindow : public Gtk::Window
{
public:
    MainWindow();   
private:
    MainWindowPane pane;
//  std::list<SlaveWindowThread> windows;       // Keeps track of all windows that have been created thus far.
    void destroyAllWindows();           // Iterates through the linked list and destroys each window.
};
#endif      //ifndef MAINWINDOW_H

/* 
 * MAIN_WINDOW.CPP
 *
 */  
#include "MainWindow.h"  
#include "MainWindowPane.h"  
#include "StartButton.h"  

MainWindow::MainWindow()// : /*list,*/ pane(/*list*/)
{
    pane;
}

void MainWindow::destroyAllWindows()
{
    //gtk_widget_destroy(*this);
    // TODO: Destroy all the other windows too.
}

/*
 * MAIN_WINDOW_PANE.H
 */  
#ifndef MAINWINDOWPANE_H  
#define MAINWINDOWPANE_H  

#include <gtkmm/box.h>
#include <gtkmm/button.h>

//#include "SlaveWindow.h"
#include "StartButton.h"

class MainWindowPane : public Gtk::HBox
{
public:
    MainWindowPane(/*&(std::list)*/);   
private:
    StartButton englishButton;      // Used to create a new RSED demo screen.
    StartButton chineseButton;      // Used to create a new RSED demo in chinese.
//  std::list<SlaveWindow> windows;     // Keeps track of all windows that have been created thus far.
    void destroyAllWindows();       // Iterates through the linked list and destroys each window.
};
#endif      //ifndef MAINWINDOWPANE_H

/* 
 * MAIN_WINDOW.CPP
 *
 */  
#include "MainWindowPane.h"  
#include "StartButton.h"  

MainWindowPane::MainWindowPane(/*&(std::list)*/) : 
    englishButton(StartButton::ENGLISH/*,&(std::list)*/), 
    chineseButton(StartButton::CHINESE/*,&(std::list)*/)
{
    pack_start(englishButton);
    englishButton.show();
    pack_start(chineseButton);
    chineseButton.show();
}

void MainWindow::destroyAllWindows()
{
    //gtk_widget_destroy(*this);
    // TODO: Destroy all the other windows too.
}
4

2 回答 2

8

未定义的引用错误意味着您要么忘记编写定义缺少的函数(通过在.cpp文件中编写实现),要么忘记将适当的目标文件或库链接到最终的二进制文件中。

在这种情况下,是后一个原因。您需要MainWindowPane.o在 makefile 的链接器命令中包含:

g++  -g -o QPI_frontend main.o MainWindow.o StartButton.o `pkg-config gtkmm-2.4 --libs`
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                        you need MainWindowPane.o in here
于 2010-08-17T21:06:36.180 回答
0

它抱怨您试图调用不存在的 MainWindowPane 的默认构造函数。

我的猜测是 MainWindowPane 只定义带参数的 ctor,并且您将其用作基类,并且您没有为派生类定义 ctor,或者您定义的 ctor 没有使用参数调用基类。

class MyDerived : public MainWindowPane
{
  public:
      MyDerived() {....}    // bad

      MyDerived(int x) : MainWindowPane(x)  // good
          {....} 

更新:

好的,忽略上面的(写在你发布代码之前)。好像在抱怨这条线”

MainWindow::MainWindow()// : /*list,*/ pane(/*list*/) 
{ 
    pane;     // Error HERE.
}

我真的不知道那条线的目的是什么。您只是在引用一个数据成员,而不用它做任何事情。应该可以删了吧。

UPDATE2: pane如果您什么都不做,将默认构造:

MainWindow::MainWindow()
{ 
}
于 2010-08-17T20:52:31.350 回答