' node'不是全球名称空间的成员吗?

发布于 2024-10-29 00:25:32 字数 5815 浏览 1 评论 0原文

我不知道代码有什么问题,但在 Visual Studio 中不断收到这些错误消息。

Error   18  error C1903: unable to recover from previous error(s);  29
Error   15  error C2039: 'Node' : is not a member of '`global namespace''       23
Error   6   error C2039: 'node_count' : is not a member of '`global namespace'' 13
Error   5   error C2039: 'node_count_' : is not a member of '`global namespace''    10
Error   3   error C2059: syntax error : '<' 
Error   13  error C2059: syntax error : '<'     23
Error   11  error C2086: 'int CS170::List' : redefinition   23
Error   7   error C2143: syntax error : missing ';' before '{'  14
Error   1   error C2143: syntax error : missing ';' before '<'  10
Error   9   error C2143: syntax error : missing ';' before '<'  23
Error   8   error C2447: '{' : missing function header (old-style formal list?) 14
Error   17  error C2588: '::~Node' : illegal global destructor      29
Error   2   error C2988: unrecognizable template declaration/definition     10
Error   12  error C2988: unrecognizable template declaration/definition     23
Error   4   error C3083: 'Node': the symbol to the left of a '::'   10
Error   14  error C3083: 'Node': the symbol to the left of a '::' must be a type    23
Error   16  error C3083: 'Node': the symbol to the left of a '::' must be a type    29
Error   10  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   23

这是头文件;

////////////////////////////////////////////////////////////////////////////////
#ifndef LIST_H
#define LIST_H
////////////////////////////////////////////////////////////////////////////////

#include <iostream> /* ostream, endl */

namespace CS170
{
    template <typename T> class List;

    template <typename T>
    std::ostream & operator<<(std::ostream & os, const List<T> &list);

    template <typename T>
    class List
    {
    public:
        // Default constructor
        List(void);

        // Copy constructor for constructing a list from an existing list
        List(const List &list);

        // Construct a list from a T array
        List(const T *array, int size);

        // Destructor
        ~List(void);

        void push_front(const T& Value); // adds the item to the front of the list
        void push_back(const T& Value);  // adds the item to the end of the list
        void pop_front(void);       // removes the first item in the list
        T front(void) const;        // retrieves the first item in the list
        int size(void) const;       // returns the number of items in the list
        bool empty(void) const;     // true if empty, else false
        void clear(void);           // clears the list

        // Overloaded assignment operator (=) for assigning one list to another
        List& operator=(const List &list);

        // Overloaded addition operator (+) for adding two lists
        List operator+(const List &list) const;

        // Overloaded addition/assignment (+=) for adding to a list "in place"
        List& operator+=(const List &list);

        // Overloaded subscript operators
        const T& operator[](int index) const;
        T& operator[](int index);

        // Output operator for printing lists (<<)
        friend std::ostream & operator<< <T> (std::ostream & os, const List &list);

        // Returns the number of Nodes that have been created
        static int node_count(void);

    private:
        // Used to build the linked list
        struct Node
        {
            Node *next;     // pointer to the next Node
            T data;         // the actual data in the node
            Node(T value);  // non-default constructor (conversion)
            ~Node(void);    // destructor
            static int node_count_; // number of Nodes created
        };

        Node *head_; // pointer to the head of the list
        Node *tail_; // pointer to the last node
        int size_;   // number of items on the list

        // All nodes are created in this method
        Node *new_node(const T& data) const;
    };

} // namespace CS170

#include "List.cpp"

#endif
////////////////////////////////////////////////////////////////////////////////

这是 cpp 文件;

namespace CS170
{

    // static members

    template <typename T>
    int List<T>::Node::node_count_ = 0;

    template <typename T>
    int List<T>::node_count(void)
    {
        return Node::node_count_;
    }


    // List::Node methods
    template <typename T>
    List<T>::Node::Node(T value) : data(value)
    {
        node_count_++;
    }

    template <typename T>
    List<T>::Node::~Node(void)
    {
        node_count_--;
    }

    // public methods

    template <typename T>
    void List<T>::clear(void)
    {
        while (!empty())
            pop_front();
    }

    // private methods

    template <typename T>
    typename List<T>::Node *List<T>::new_node(const T& data) const
    {
        Node *node = new Node(data); // create the node
        node->next = 0;              // no next pointer yet
        return node;
    }

} // namespace CS170


// non-members

#include <iomanip>

template <typename T>
std::ostream &CS170::operator<<(std::ostream & os, const CS170::List<T> &list)
{
    // Start at the top
    typename CS170::List<T>::Node *pnode = list.head_;

    // Print each item
    while (pnode != 0)
    {
        os << std::setw(4) << pnode->data;
        pnode = pnode->next;
    }
    os << std::endl;
    return os;
}

谢谢...

I don't know whats wrong with the code, but keep getting these error messages in visual studio.

Error   18  error C1903: unable to recover from previous error(s);  29
Error   15  error C2039: 'Node' : is not a member of '`global namespace''       23
Error   6   error C2039: 'node_count' : is not a member of '`global namespace'' 13
Error   5   error C2039: 'node_count_' : is not a member of '`global namespace''    10
Error   3   error C2059: syntax error : '<' 
Error   13  error C2059: syntax error : '<'     23
Error   11  error C2086: 'int CS170::List' : redefinition   23
Error   7   error C2143: syntax error : missing ';' before '{'  14
Error   1   error C2143: syntax error : missing ';' before '<'  10
Error   9   error C2143: syntax error : missing ';' before '<'  23
Error   8   error C2447: '{' : missing function header (old-style formal list?) 14
Error   17  error C2588: '::~Node' : illegal global destructor      29
Error   2   error C2988: unrecognizable template declaration/definition     10
Error   12  error C2988: unrecognizable template declaration/definition     23
Error   4   error C3083: 'Node': the symbol to the left of a '::'   10
Error   14  error C3083: 'Node': the symbol to the left of a '::' must be a type    23
Error   16  error C3083: 'Node': the symbol to the left of a '::' must be a type    29
Error   10  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   23

Here is the header file;

////////////////////////////////////////////////////////////////////////////////
#ifndef LIST_H
#define LIST_H
////////////////////////////////////////////////////////////////////////////////

#include <iostream> /* ostream, endl */

namespace CS170
{
    template <typename T> class List;

    template <typename T>
    std::ostream & operator<<(std::ostream & os, const List<T> &list);

    template <typename T>
    class List
    {
    public:
        // Default constructor
        List(void);

        // Copy constructor for constructing a list from an existing list
        List(const List &list);

        // Construct a list from a T array
        List(const T *array, int size);

        // Destructor
        ~List(void);

        void push_front(const T& Value); // adds the item to the front of the list
        void push_back(const T& Value);  // adds the item to the end of the list
        void pop_front(void);       // removes the first item in the list
        T front(void) const;        // retrieves the first item in the list
        int size(void) const;       // returns the number of items in the list
        bool empty(void) const;     // true if empty, else false
        void clear(void);           // clears the list

        // Overloaded assignment operator (=) for assigning one list to another
        List& operator=(const List &list);

        // Overloaded addition operator (+) for adding two lists
        List operator+(const List &list) const;

        // Overloaded addition/assignment (+=) for adding to a list "in place"
        List& operator+=(const List &list);

        // Overloaded subscript operators
        const T& operator[](int index) const;
        T& operator[](int index);

        // Output operator for printing lists (<<)
        friend std::ostream & operator<< <T> (std::ostream & os, const List &list);

        // Returns the number of Nodes that have been created
        static int node_count(void);

    private:
        // Used to build the linked list
        struct Node
        {
            Node *next;     // pointer to the next Node
            T data;         // the actual data in the node
            Node(T value);  // non-default constructor (conversion)
            ~Node(void);    // destructor
            static int node_count_; // number of Nodes created
        };

        Node *head_; // pointer to the head of the list
        Node *tail_; // pointer to the last node
        int size_;   // number of items on the list

        // All nodes are created in this method
        Node *new_node(const T& data) const;
    };

} // namespace CS170

#include "List.cpp"

#endif
////////////////////////////////////////////////////////////////////////////////

and here is the cpp file;

namespace CS170
{

    // static members

    template <typename T>
    int List<T>::Node::node_count_ = 0;

    template <typename T>
    int List<T>::node_count(void)
    {
        return Node::node_count_;
    }


    // List::Node methods
    template <typename T>
    List<T>::Node::Node(T value) : data(value)
    {
        node_count_++;
    }

    template <typename T>
    List<T>::Node::~Node(void)
    {
        node_count_--;
    }

    // public methods

    template <typename T>
    void List<T>::clear(void)
    {
        while (!empty())
            pop_front();
    }

    // private methods

    template <typename T>
    typename List<T>::Node *List<T>::new_node(const T& data) const
    {
        Node *node = new Node(data); // create the node
        node->next = 0;              // no next pointer yet
        return node;
    }

} // namespace CS170


// non-members

#include <iomanip>

template <typename T>
std::ostream &CS170::operator<<(std::ostream & os, const CS170::List<T> &list)
{
    // Start at the top
    typename CS170::List<T>::Node *pnode = list.head_;

    // Print each item
    while (pnode != 0)
    {
        os << std::setw(4) << pnode->data;
        pnode = pnode->next;
    }
    os << std::endl;
    return os;
}

Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隱形的亼 2024-11-05 00:25:32

struct Node0 应该是 struct Node

struct Node0 should be struct Node.

下雨或天晴 2024-11-05 00:25:32

您将结构命名为 Node0,但随后尝试将其引用为 Node

You named your struct Node0 but then attempted to refer to it as Node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文