我应该实现堆栈的功能,我得到了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()'|
的main.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 */
}
StackOfNodees.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
Node.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
StackOfNodees.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
非常感谢任何帮助。谢谢!
答案 0 :(得分:1)
您没有定义模板类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文件会降低代码的可读性 ,因为它会破坏阅读流程。如果您仍然需要以这种方式拆分代码,那么您需要将.hpp包含在.h文件中,并在.h文件底部包含#include "StackOfNodes.hpp"
指令(这是一种非常规的方法)。
答案 1 :(得分:1)
在编写模板类时,必须在声明本身所在的同一文件中提供模板类的定义,以便编译器在编译时可以实例化实际的类。
在这种特定情况下,将StackOfNodees.hpp
特定的一个更改为常规类型StackOfNodees.h
之后,将int
中的定义放回文件T
中的模板类声明中。 This是一个关于模板类格式的简单教程。