c++ - undefined reference 一般错误

标签 c++

我应该实现堆栈的功能,我得到了 main.cpp 文件并留下来编写其他文件,即 StackofNodes.hpp 和 .h。这些依赖于另外两个文件 Node.hpp & .h。当我尝试构建程序时,我在提供给我的 main.cpp 文件中遇到错误。这是:

obj/Debug/main.o||In function `main':|
|6|undefined reference to `StackOfNodees<int>::StackOfNodees()'|
|12|undefined reference to `StackOfNodees<int>::push(int)'|
|17|undefined reference to `StackOfNodees<int>::size() const'|
|24|undefined reference to `StackOfNodees<int>::pop()'|

主要.cpp

   #include <iostream> //std::cout std::cin
    #include "StackOfNodees.h" //StackOfNodes

    int main()
    {
        StackOfNodees<int> myStack; //Create an empty stack
        int sizeOfStack;    //int we'll use later to store the size of the stack

        //push some numbers onto the stack
        for(int i = 1; i <= 10; i++)
        {
            myStack.push( i * 5 );
        }


        //Store the size of the stack before popping anything
        sizeOfStack = myStack.size();

        std::cout << "There are " << sizeOfStack << " items on the stack" << std::endl;

        //Think about why we don't use i<myStack.size()
        for(int i = 0; i < sizeOfStack; i++)
        {
            std::cout << "Popping the top: " << myStack.pop() << std::endl;
        }

            /* while(!myStack.isEmpty()) is another valid way to pop all the contents of the stack */

    }

节点堆栈.hpp

#ifndef STACKOFNODEES_HPP_INCLUDED
#define STACKOFNODEES_HPP_INCLUDED

StackOfNodees::StackOfNodees()
{
    m_top=nullptr;
    m_size=0;
}

bool StackOfNodees::isEmpty() const
{
    if (m_size==0)
    {
        return true;
    }
    return false;
}

int StackOfNodees::size() const
{
    return m_size;
}

void StackOfNodees::push(int value)
{
    Node* Node1 = new Node;
    Node1 -> m_previous = m_top;
    m_top=Node1;
    Node1 -> m_value = value;
    ++m_size;
}

int StackOfNodees::pop()
{
    // initialize
    int returnval = 0;

    if (!isEmpty())
    {
        // fetch the value from the top one
        returnval = m_top -> m_value;

        Node* temp = m_top;

        //move m_top down to previous box.
        m_top = m_top -> m_previous;

        //delete the popped one
        delete temp;

        m_size--;
    }
    else
    {
        // you may want to throw an exception here for popping a empty stack
    }

    return returnval;
}




#endif // STACKOFNODEES_HPP_INCLUDED

节点.h

#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED

template<typename T>
Node<T>::Node()
{
    Node.setPrevious(nullptr);
    Node.setValue();
}

//initiation of getters
    template<typename T>
    T Node::getValue()
    {
        return m_value;
    }//end getValue

    template<typename int>
    int Node::getPrevious()
    {
        return m_previous;
    }//end getPrevious

//initiation of setters
    template<typename void>
    void Node::setValue(T value)
    {
        m_value =  value;
    }//end setValue

    template<typename void>
    void Node::setPrevious(Node<T>* previous)
    {
        Node<T>* previous = m_previous;
    }//end setPrevious

节点堆栈.h

#ifndef STACKOFNODEES_H_INCLUDED
#define STACKOFNODEES_H_INCLUDED
#include "Node.h"

template<typename T>
class StackOfNodees
{
private:
    Node<T>* m_top;
    int m_size;

public:
    StackOfNodees();
    bool isEmpty() const;
    int size() const;
    void push(T value);
    T pop();
};

#endif // STACKOFNODEES_H_INCLUDED

任何帮助将不胜感激。谢谢!

最佳答案

您没有定义模板类 StackOfNodees 的方法。 你需要有

template<typename T>
StackOfNodees<T>::StackOfNodees()
{
    m_top=nullptr;
    m_size=0;
}

而不是

StackOfNodees::StackOfNodees()
{
    m_top=nullptr;
    m_size=0;
}

更改所有定义并将它们从 .hpp 移动到 StackOfNodees.h 文件中。您不需要额外的 .hpp 文件。

例子:

// File a.h
template<typename T>
class A
{
public:
    A();
    // the rest of the class ....
};

template<typename T>
A<T>::A()
{
    // ...
}

文件 app.cpp

#include "a.h"
void main()
{
    A<int> a;
...
}

额外的 .hpp 文件会降低代码的可读性,因为它会打断阅读流程。如果您仍然需要以这种方式拆分代码,那么您需要在 .h 文件底部使用 #include "StackOfNodes.hpp" 指令将 .hpp 包含到 .h 文件中(一个非常规的方法)。

关于c++ - undefined reference 一般错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22240611/

相关文章:

c++ - 查找排序范围内元素数量的最快方法是什么?

c++ - 使用包含托管类的/CLR 标志构建的 native dll

c++ - C++中的template <int…>想法可能吗?

c++ - OpenCV HoughLinesP

c++ - 无法将参数 2 从 'mxArray **' 转换为 'mwArray &'

c++ - 了解运算符重载和迭代器为什么会打印出 “wrhrwwr”?

c++ - 初始化并传递数组

c++ - 为什么 malloc(0) 的返回值是实现定义的?

c++ - 传递一个成员函数作为 C++ 标准库算法的比较运算符

c++ - 为什么 myClassObj++++ 不会产生编译错误 : '++' needs l-value just as buildin type do?