常规C ++ Node类错误

时间:2014-03-07 05:06:59

标签: 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 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|

的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

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

    template<typename T>
    bool StackOfNodees<T>::isEmpty() const
    {
        if (m_size==0)
        {
            return true;
        }
        return false;
    }

    template<typename T>
    int StackOfNodees<T>::size() const
    {
        return m_size;
    }

    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;
    }

    template<typename T>
    int StackOfNodees<T>::T 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
        {
            //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>

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

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
#include "StackOfNodees.hpp"

Node.h

#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

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

  1. 您需要在Node.h
  2. 中加入StackOfNodees.h
  3. 您的模板类'成员函数定义必须位于头文件中。
  4. pop()方法中,您尝试使用m_previous类的Node,这是一个私有成员。您需要将成员公开,添加公共访问器函数,或使堆栈类成为节点类的朋友。
  5. 与#3相同,但与m_value相同。
  6. void StackOfNodees<T>::push(T)行应为void StackOfNodees<T>::push(T value)
  7. 从第T
  8. 中删除Node1 -> m_value = T value;

    如果不是所有这些错误,这些都应该清除。

答案 1 :(得分:0)

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;
}

这可能有多种原因。

  • 编译器无法找到Node类,请确保#include定义它的标头。

    Node1 -> m_value = T value;
    

上面的代码是什么,你必须在使用之前定义变量。在C ++中,你不能在同一个语句中定义和使用varibale。使用如下

    Node1 -> m_value = value;