'Node' is not a member of global namespace?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
////////////////////////////////////////////////////////////////////////////////
#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;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
closed account (zwA4jE8b)
declare your node struct outside of the class.

1
2
3
4
5
6
7
8
9
10
11
namespace CS170
{
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
        };
//rest of your code here 


inside the namespace but outside the class

you might have to put it outside the namespace, not sure
Last edited on
closed account (zwA4jE8b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

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

#include <iostream> /* ostream, endl */
#include "List.cpp"

namespace CS170
{
    template <typename T> class List;

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

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

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

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


also, im not sure if you need this line template <typename T> class List;
Thanks for the answer CreativeMFS, but the problem is I am not allowed to change anything inside the .h file. There has to be another way to make it compile.
closed account (zwA4jE8b)
sheesh, wierd assignment, it might not be able to compile, what version of VS was it made in and what version are you using. I don't know enough about the compiler options to even go there. Why can't you change it, is that your assignment, to learn about the compiler options or something?

List(const List &list); is wrong because &list should be &otherlist.

Just seems like there is a lot wrong with the header, but I am a fairly new programmer so I can not be %100 certain.
I tried it in both visual studio 2008 and 2010. got the same errors.

List(const List &list); is just a copy constructor there is nothing wrong with that statement.
it takes a constant reference to a List object which allows the user to construct a new object.
such as;


1
2
3
4

List a;
List b(a);
Last edited on
closed account (zwA4jE8b)
oh sorry, i didn't realize the capital L before. I was looking at List and list as if they were the same.

I notice that the first error is line 10 template <typename T> class List;

that is something I have not seen before, line 10 and line 15/16 are the same lines of code, seems like the class is being declared twice.

I have a linked list header that I wrote and originally I had the node struct in the class and had to move it out of the class for my program to work. That is the only way I know.

Good Luck.
Topic archived. No new replies allowed.