对“Class::Class()”的未定义引用
我正在编写一个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.
}
I am writing a GTKmm window program; the main window creates two buttons, one for English and one for Chinese. The user can click on the button to bring up a different window in the appropriate language. Currently I am having trouble initializing the multiple-item container inside the main window. It is an object of type MainWindowPane, which inherits Gtk::HBox.
When I try to make, the compiler issues the following error:
$ 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
I am using the latest version of gcc with pkg-config to include the proper libraries. I am also a java person.
/*
* 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.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
未定义的参考错误意味着您要么
忘记写定义缺少的函数
(通过在
.cpp
文件中编写实现),或者您忘记将适当的目标文件或库链接到最终的二进制文件中。
在这种情况下,就是后来的原因了。
您需要在 makefile 的链接器命令中包含
MainWindowPane.o
:Undefined reference errors mean you either
forgot to write define the missing function
(by writing an implementation in the
.cpp
file),or you forgot to link the appropriate object file or library into the final binary.
In this case, it's the later reason.
You need to include
MainWindowPane.o
in the linker command in your makefile:它抱怨您试图调用不存在的 MainWindowPane 的默认构造函数。
我的猜测是 MainWindowPane 仅定义带有参数的 ctor,并且您将其用作基类,并且您要么没有为派生类定义 ctor,要么您定义的 ctor 没有定义t 使用参数调用基类。
更新:
好的,忽略上面的内容(在发布代码之前编写)。它似乎在抱怨这一行”,
我真的不知道该行的目的是什么。您只是引用一个数据成员,而不对其进行任何操作。它可能会被删除。
更新2:
如果您不执行任何操作,将默认构造
pane
:It's complaining that you are trying to call a default constructor for MainWindowPane that doesn't exist.
My guess is that MainWindowPane only defines ctors with parameters, and you are using it as a base class, and you either didn't define a ctor for the derived class, or the ctor you did define didn't call the base with parameters.
UPDATE:
OK, ignore the above (written before you posted the code). It seems to be complaining about this line"
And I really have no idea what the purpose of that line is. You are just referencing a data member, without doing anything with it. It can probably be deleted.
UPDATE2:
pane
will be default constructed if you do nothing: