为什么我得到一个未定义的引用错误

时间:2014-02-28 01:24:53

标签: c++ error-handling compiler-errors

我的代码目前出现了编译错误,它在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;
}

2 个答案:

答案 0 :(得分:2)

我的猜测是你已经使用类似的构造函数声明了框:

class box {
public:
    box(void);

};

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

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

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

答案 1 :(得分:1)

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

相关问题