【问题标题】:Getting "undefined reference" in QT Creator when calling defined function调用已定义函数时在 QT Creator 中获取“未定义引用”
【发布时间】:2012-03-27 11:00:08
【问题描述】:

所以,我正在阅读这本书:Jasmin Blanchette 的 C++ GUI Programming with Qt 4, Second Edition; Mark Summerfield,为了学习 GUI 编程。在按照本书的步骤创建一个简单的电子表格应用程序时,我得到一个“未定义的参考” 当我尝试编译电子表格小部件时出错。

所有错误似乎都源于Cell *Spreadsheet::cell(int row, int column)函数:

Cell *Spreadsheet::cell(int row, int column) const
{
    return static_cast<Cell *>(item(row, column));
}

这是从电子表格.cpp 抱怨的第一个函数

QString Spreadsheet::formula(int row, int column) const {
    Cell *c = cell(row, column);
    if (c) {
        return c->formula();
    } else {
        return "";
    }
}

Cell *c = cell(row, column); 所在行发送错误:/home/axel/QtSDK/Code/Spreadsheet/spreadsheet.cpp:-1: error: undefined reference to `Cell::Cell()'

在调用cell(row, column) 的任何地方都会发生这种情况。该函数本身在 private 部分的 spreadhseet 标头中定义为:Cell *cell(int row, int column) const;

抱歉,如果我对 C++ 编程有点陌生。

这是我的 .pro 文件

TEMPLATE = app
CONFIG += console
CONFIG += qt

SOURCES += main.cpp \
           spreadsheet.cpp

HEADERS += \
           spreadsheet.h

如果我遗漏了什么,我会尽快添加。

谢谢, 阿克塞尔

【问题讨论】:

  • 您的.pro 文件是什么样的?
  • 添加了.pro文件^^

标签: c++ qt


【解决方案1】:

Cell 类的定义不存在。它至少应该是cell.h,并且应该包含在电子表格.cpp 中,或者它的代码可以直接包含在spresheed.h.cpp 中,这种可能性较小。无论如何,错误意味着缺少 Cell 构造函数。

【讨论】:

  • 好吧,我在电子表格.cpp 的包含部分中有 #include "../cell/cell.h",还是应该包含对 cell.h 的另一个引用?
  • 在项目中添加文件cell.h和cell.cpp。为此,请单击项目树根(左侧面板)上的右键并选择add existing files
  • 恭喜!然后请按绿色大 V 来检查我接受的答案,并享受 QT 和 c++ 的乐趣))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多