为什么我会收到未定义的引用错误

Why am I getting an undefined reference error

本文关键字:引用 错误 未定义 为什么      更新时间:2023-10-16

我的代码目前正在StackOfBoxes.cpp的第40行找到编译错误。该错误是未定义的引用错误:

StackOfBoxes.cpp:40:未定义对Box::Box();的引用

我不确定该怎么做才能进行调试。任何帮助将不胜感激。

bool StackOfBoxes::isEmpty() const
{
    if (m_size == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}
int StackOfBoxes::size() const
{
    return m_size;
}
void StackOfBoxes::push(int value)
{
    Box* box = m_top;
    m_top = new Box();
    m_top->m_value = value;
    m_top->m_previous = box;
    // increase m_size by 1;
    m_size += 1;
}
int StackOfBoxes::pop()
{
    // Create a Box pointer (Box* temp) and store the value of m_previous using m_top
    Box* temp = m_top->m_previous;
    // Similarly store the m_value using m_top into the Integer variable 
    int x = m_top->m_value;
    // Delete top of the stack 
    delete m_top;
    // make the current Box pointer your new m_top
    m_top = temp;
    return x;
}

我的猜测是你已经用一个构造函数声明了盒子,如下所示:

class box {
public:
    box(void);
};

但后来没有继续定义构造函数,例如:

box::box(void)
{
    ....
}

要么是那个,要么是那个文件没有被链接。

我猜这是第 40 行m_top = new Box(); 此错误的可能原因是您尚未定义默认构造函数。