I was starting to learn to use class object in different .cpp file..
What I did:
- Created a class of node in one file and saved it with node.h
- Created another file with name node_pair.cpp and included node.h
- Created a function named
pair()and called it frommain()
Now, I want to ask two things:
- I am getting
error: reference to ‘pair’ is ambiguous
Here is the code for node.h file
#include "iostream"
#include"stdlib"
using namespace std;
class node
{
int data;
public:
node *next;
int insert(node*);
node* create();
int display(node *);
} *start = NULL, *front, *ptr, n;
int node::insert(node* np)
{
if (start == NULL)
{
start = np;
front = np;
}
else
{
front->next = np;
front = np;
}
return 0;
}
node* node::create()
{
node *np;
np = (node*)malloc(sizeof(node)) ;
cin >> np->data;
np->next = NULL;
return np;
}
int node::display(node* np)
{
while (np != NULL)
{
cout << np->data;
np = np->next;
}
return 0;
}
int main_node()
{
int ch;
cout << "enter the size of the link list:";
cin >> ch;
while (ch--)
{
ptr = n.create();
n.insert(ptr);
}
n.display(start);
cout << "\n";
return 0;
}
and here is the code for node_pair.cpp
#include"iostream"
#include"node.h"
using namespace std;
node obj;
int pair()
{
node* one, *two, *save;
one = start;
two = start->next;
while (two != NULL)
{
save = two->next;
two->next = one;
one->next = save;
}
}
int main()
{
main_node();
pair();
obj.display(start);
return 0;
}
What should I do to resolve this error
And now the second problem
2. I want to keep node* next pointer to be private in node class but if I do so then I will not get access for it in pair() function.
Please answer, thanks in advance.
using namepsace std;and there is a std::pair. Your error is the textbook reason to not using namespace, you don't know everything in thestdnamespace and will likely get a collision eventually.String.classin Java. In C++, classes are not objects.