Hi, I have some problem to implement the constructors from set.h. There was no problem to implement the empty constructor. But as for:
Set(int a[], int n);
I have some problem. It is supposed to use insert() function from node.cpp. And since the class Set is friend a friend to class Node it should have access to the private members in class Node right?
Well, anyway I tried to implement Set(int a[], int n);
with following code
Set::Set(int a[], int n) {
Set p;
p.insert(a[0]);
}
and I get compile error saying "undefined reference to Set::insert(int);
Below is the code I have in use.
set.h
#include "node.h"
#include <iostream>
class Set {
public:
Set ();
Set (int a[], int n);
Set (const Set& b);
~Set ();
private:
Node *header;
friend std::ostream& operator << (std::ostream& os, const Set& b);
void insert (int value);
void del (int value);
};
node.h
#include <iostream>
class Node {
public:
Node(int, Node*); //constructor
Node *insert(int value); //function
private:
int value;
Node *next;
friend class Set;
friend std::ostream& operator << (std::ostream &os, const Set &theSet);
};
node.cpp
#include "node.h"
#include <cassert> //assert
Node::Node (int nodeVal, Node *nextPtr) : value (nodeVal), next (nextPtr)
{
// std::cout << "Constructor Node" << std::endl;
}
Node *Node::insert (int newValue) {
next = new Node (newValue, next);
assert (next != 0);
return next;
}