Problems with Interface/Implementation Files

Hi all,

I am currently self-studying header files, implementation files and the like. The program that I'm trying to write takes a singly linked list of integers and reverses the order of them. From everything that I have read, I should have a header and source file for each class, struct etc. The problem that I have is getting the files to "see" each other, as in, #include... The error I am getting is "Node is undefined". The IDE I am using is Microsoft Visual C++ 2010 Express. If anyone can point me in the right direction with this, I would be greatly appreciative. The files I have thus far are below. Once I can get this code to compile I will be adding a stack class and incorporating it into the code. Thank you in advance for any and all help.

Node.h
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
#ifndef NODE_H
#define NODE_H

namespace node
{
	class Node
	{
		public:
			//CONSTRUCTORS
			Node();
			Node(int, Node*);

			//DESTRUCTOR
			~Node();

			//ACCESSORS
			int getData() const;
			Node* getLink() const;

			//MUTATORS
			void setData(int);
			void setLink(Node*);

		private:
			int data;
			Node *link;
	};
}

#endif 


Node.cpp
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
#include <iostream>
#include "Node.h"

namespace node
{
	//CONSTRUCTORS
	Node::Node() : data(0), link(nullptr)
	{

	}
	Node::Node(int value, Node *ptr) : data(value), link(ptr)
	{

	}

	//DESTRUCTOR
	Node::~Node()
	{
		if(link != nullptr)
			delete link;
	}

	//ACCESSORS
	int Node::getData() const
	{
		return (data);
	}
	Node* Node::getLink() const
	{
		return (link);
	}

	//MUTATORS
	void Node::setData(int value)
	{
		data = value; 
	}
	void Node::setLink(Node *ptr)
	{
		link = ptr;
	}
}


Linked List.h
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
#ifndef LINKED_LIST_H
#define LINKED_LIST_H

#include "Node.h"

namespace linkedlist
{
	class LinkedList
	{
		private:
			Node *head;

		public:
			//CONSTRUCTORS
			LinkedList();
			LinkedList(const LinkedList &);

			//DESTRUCTOR
			~LinkedList();

			//ACCESSORS
			bool empty() const;
			Node* getHead() const;
			Node* search(int) const;

			//MUTATORS
			void insertHead(int);
			void insert(Node*, int);
			void append(int);
			void remove(int);

			//OVERLOADED OPERATORS
			void operator=(const LinkedList &);

			friend std::ostream& operator<<(std::ostream &, const LinkedList &);

	};
}

#endif 


Linked List.cpp
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
#include <iostream>
#include "Linked List.h"

namespace linkedlist
{
	//CONSTRUCTORS
	LinkedList::LinkedList() : head(nullptr)
	{

	}
	LinkedList::LinkedList(const LinkedList &)
	{

	}

        //DESTRUCTOR
	LinkedList::~LinkedList()
	{

	}

	//ACCESSORS
	bool LinkedList::empty() const
	{
		return (head == nullptr);
	}
	Node* LinkedList::getHead() const
	{
		return (head);
	}
	Node* LinkedList::search(int value) const
	{
		using namespace node;

		for(Node *iterator = getHead(); iterator != nullptr; iterator = iterator->getLink())
		{
			if(iterator->getData() == value)
				return (iterator);
		}

		return (nullptr);
	}

	//MUTATORS
	void LinkedList::insertHead(int value)
	{
		using namespace node;

		Node *temp(value, nullptr);

		if(empty())
			temp->setLink(head);

		head = temp;
	}
	void LinkedList::insert(Node *after, int value)
	{

	}
	void LinkedList::append(int value)
	{

	}
	void LinkedList::remove(int value)
	{

	}

        //OVERLOADED OPERATORS
	void LinkedList::operator=(const LinkedList &rhs)
	{

	}
	std::ostream& operator<<(std::ostream &ostr, const LinkedList &list)
	{
		using namespace node;

		for(Node *iterator = list.getHead(); iterator != nullptr; iterator = iterator->getLink())
			ostr << iterator->getData() << std::endl;
	}
}


Use a qualified name for Node. For instance:

1
2
3
4
5
6
namespace linkedlist
{
	class LinkedList
	{
		private:
			node::Node *head;


Also consider revising the use of namespaces in your code. There is not much point in pacing each class in a separate namespace (which has the same name as the class with the case changed.)
Thank you JLBorges. I do agree that the use of the namespaces is overkill. I recently learned about them as I was trying to incorporate them into my code so that I could actually use them and it not be something that I just read about in one of my textbooks. If I may ask, if I remove the namespaces, is the code somewhat "close" as to how a professional software developer would go about making a simple program like this? Thanks again!
> if I remove the namespaces, is the code somewhat "close" as to how
> a professional software developer would go about making a simple program like this?

As far as names and namespaces go, a professional software developer might do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
namespace utility // or whatever
{
    class linked_list
   {
          private:
               class node // node is an implementation detail that does not appear 
                          // in the interface of linked_list and is not directly accessible by users
               {
                     // ...
               };
   }; 
}


There are may other things that a professional software developer might do. For self-study of header files, implementation files and the like, focus on the placement and visibility of names.
For that, this much should be adequate.

Caveat: assuming that 'professional software developer' understands C++ well (there are too many who don't).
Topic archived. No new replies allowed.