【问题标题】:undefined reference to `Class::Class()'对“Class::Class()”的未定义引用
【发布时间】:2011-03-31 05:26:30
【问题描述】:

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

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

$ 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.
}

【问题讨论】:

    标签: c++ gcc gtk g++ gtkmm


    【解决方案1】:

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

    我的猜测是 MainWindowPane 只定义了带参数的 ctor,并且您将其用作基类,并且您要么没有为派生类定义 ctor,要么您已经定义了 ctor define 没有用参数调用基类。

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

    更新:

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

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

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

    更新2: 如果您什么都不做,pane 将被默认构造:

    MainWindow::MainWindow()
    { 
    }
    

    【讨论】:

    • 一开始我忘了输入我的代码。哎呀!我现在已经编辑了。可以看到这两个构造函数我都在cpp文件中定义了MainWindowPane的默认构造函数。希望代码使它更清晰一点。我觉得我在某个地方的语法上犯了一些小错误(Java 程序员很容易陷入这种错误!)。
    • 该行试图调用窗格的默认构造函数。我在使用 pane() 时遇到了不同的错误;我将尝试的构造函数调用移到函数体中,因为当我尝试在初始化列表中调用窗格时遇到了同样的错误。后来我忘了把它移回去。这段代码我得到了同样的错误: ... MainWindow::MainWindow() : pane() { } 。使用此代码: MainWindow::MainWindow() : pane {} ... 我收到此错误: MainWindow.cpp:9: warning: extended initializer lists only available with -std=c++0x or -std=gnu++ 0x' ...感谢您迄今为止的所有帮助。
    【解决方案2】:

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

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

    g++  -g -o QPI_frontend main.o MainWindow.o StartButton.o `pkg-config gtkmm-2.4 --libs`
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                            you need MainWindowPane.o in here
    

    【讨论】:

    • 就是这样。将源代码添加到我的 makefile 中,代码编译成功。谢谢!现在,开始运行时调试...
    • @ILikeFood,记得点击对你有用的答案旁边的复选标记来接受答案。
    • 啊,谢谢。我也尝试过投票,但我的帐户太新了。
    猜你喜欢
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多