1

I was starting to learn to use class object in different .cpp file..

What I did:

  1. Created a class of node in one file and saved it with node.h
  2. Created another file with name node_pair.cpp and included node.h
  3. Created a function named pair() and called it from main()

Now, I want to ask two things:

  1. 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.

3
  • 1
    How is that valid C? Also, only one question per question. Next, what the ... does this have to do with node.js? Commented Aug 22, 2014 at 17:11
  • 1
    it's because you have 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 the std namespace and will likely get a collision eventually. Commented Aug 22, 2014 at 17:21
  • C++ does not have "class objects". A "class object" would be something like String.class in Java. In C++, classes are not objects. Commented Aug 22, 2014 at 17:41

2 Answers 2

2

Some tips:

  • Your function pair clashes with std::pair.
  • Don't use using namespace std;. It's a bad practice and creates errors like this.
  • Mixing node.js and C++ is an advanced topic. If you just begun to learn OOP, I recommend that you stick to pure C++.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your use full suggestion... i will definitely keep this in mind
0

You have a name collision with your pair() and std::pair.

Best practice is to avoid using the entire namespace std -- just use what you need. For example, if you only need std::cin you can use using std::cin; to add it to the global namespace but not std::pair. You can also replace all instances of cin with std::cin instead.

If you really want to use the entire namespace std you can also create your own namespace to put your code in -- in particular, you'd need to put your pair() in this new namespace. You do it like this:

namespace mynamespace {
  int pair() {
    // ...
  }

// ... other code in mynamespace
} // end mynamespace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.