C++ 一般C&x2B+;节点类错误

C++ 一般C&x2B+;节点类错误,c++,C++,我应该实现一个堆栈的功能,我得到了main.cpp文件,剩下来写其他文件,即StackofNodes.hpp和.h。这些依赖于另外两个文件Node.hpp和.h。当我试图构建这个程序时,我得到了以下错误 ||=== Lab04, Debug ===| /StackOfNodees.hpp||In member function ‘void StackOfNodees<T>::push(T)’:| /StackOfNodees.hpp|30|error: expected ty

我应该实现一个堆栈的功能,我得到了main.cpp文件,剩下来写其他文件,即StackofNodes.hpp和.h。这些依赖于另外两个文件Node.hpp和.h。当我试图构建这个程序时,我得到了以下错误

    ||=== Lab04, Debug ===|
/StackOfNodees.hpp||In member function ‘void StackOfNodees<T>::push(T)’:|
/StackOfNodees.hpp|30|error: expected type-specifier before ‘Node’|
/StackOfNodees.hpp|33|error: expected primary-expression before ‘value’|
/StackOfNodees.hpp|33|error: expected ‘;’ before ‘value’|
/StackOfNodees.hpp|38|error: expected initializer before ‘pop’|
/StackOfNodees.hpp||In instantiation of ‘void StackOfNodees<T>::push(T) [with T = int]’:|
/main.cpp|12|required from here|
/Node.h|10|error: ‘Node<int>* Node<int>::m_previous’ is private|
/StackOfNodees.hpp|31|error: within this context|
/Node.h|11|error: ‘int Node<int>::m_value’ is private|
/StackOfNodees.hpp|33|error: within this context|
| |===Lab04,调试===|
/stackOfNodes.hpp |在成员函数“void stackOfNodes::push(T)”中:|
/StackOfNodes.hpp | 30 |错误:“节点”之前应为类型说明符|
/StackOfNodes.hpp | 33 |错误:“value”之前应为主表达式|
/StackOfNodes.hpp | 33 |错误:应为“;”在“价值”之前|
/StackOfNodes.hpp | 38 |错误:应在“pop”之前使用初始值设定项|
/hpp | |在“void stackOfNodes::push(T)[with T=int]的实例化中:|
/main.cpp | 12 |从这里开始需要|
/Node.h | 10 |错误:“Node*Node::m|u previous”是私有的|
/stackOfNodes.hpp | 31 |错误:在此上下文中|
/Node.h | 11 |错误:“int Node::m|u value”是私有的|
/StackOfNodes.hpp | 33 |错误:在此上下文中|
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 */

}
#包括//std::cout std::cin
#包括“StackOfNodes.h”//StackOfNodes
int main()
{
StackOfNodes myStack;//创建一个空堆栈
int-sizeOfStack;//我们稍后将使用int来存储堆栈的大小
//将一些数字推到堆栈上
对于(int i=1;i
  • 您需要在
    stackofnodes.h
    中包含
    Node.h
  • 模板类的成员函数定义必须在头文件中
  • 在您的
    pop()
    方法中,您试图使用
    Node
    类的
    m_previous
    ,该类是私有成员。您需要将该成员设置为公共成员,添加公共访问器函数,或将堆栈类设置为节点类的朋友
  • 与#3相同,但具有
    m#u值
  • void stackOfNodes::push(T)
    应该是
    void stackOfNodes::push(T值)
  • Node1->m_value=T value;
  • 这些错误即使不是全部,也应该得到大部分的澄清

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

    上面的行是什么,在使用之前必须定义变量。在C++中,不能在同一语句中定义和使用VALUBALE。

        Node1 -> m_value = value;
    

    你能看一下我添加到问题中的
    stackOfNodes.h
    吗?我应该添加@Claptrap提到的内容吗?如果是在哪里?是的,你还需要将方法定义添加到头文件中。对于模板,定义必须在头文件中。好的,那么在我的
    节点.h
    中,我有
    getPrevious
    ,我可以用作
    公共访问或
    m_previous
    @rubito是的,您还需要一个访问器来设置
    m_值
    ,因为这也是私有的。为此,我将
    T getValue();
    作为公共。我使用了它,它清除了大部分错误。现在我只剩下“error:expected type specifier before'Node'”和“stackOfNodes.hpp | 39 |错误:应在'pop'之前使用初始值设定项”。此外,它还表示'Node1->getPrevious()=m|u top;'需要赋值的左操作数
    #ifndef NODE_H_INCLUDED
    #define NODE_H_INCLUDED
    
    
    template<typename T>
    
    class Node
    {
        private:
            Node<T>* m_previous;
            T m_value;
    
        public:
            Node();
            T getValue();
            Node<T>* getPrevious();
    
            void setValue(T value);
            void setPrevious(Node<T>* next);
    
    };
    
    
    #endif // NODE_H_INCLUDED
    
    template<typename T>
    void StackOfNodees<T>::push(T)
    {
        Node<T>* Node1 = new Node;
        Node1 -> m_previous = m_top;
        m_top=Node1;
        Node1 -> m_value = T value;
        ++m_size;
    }
    
    Node1 -> m_value = T value;
    
        Node1 -> m_value = value;