Unknown scope in pair.h

When I made a small change to get it to check the syntax, I am getting unknown scope:


#ifndef PAIR_H
#define PAIR_H
#endif

#include <bool.h>

template<class T1, class T2>
struct pair {
    T1 first;
    T2 second;
    pair() : first(T1()), second(T2()) {}
    pair(const T1& a, const T2& b) : first(a), second(b) {}
    friend void destroy(pair<T1, T2>* p) {
//Ed Y.      p->~pair();
    }
};

template<class T1, class T2>
inline iBOOL operator==(const pair<T1, T2>& x, const pair<T1, T2>& y) { 
    return x.first == y.first && x.second == y.second; 
}

template<class T1, class T2>
inline iBOOL operator<(const pair<T1, T2>& x, const pair<T1, T2>& y) { 
    return x.first < y.first || (!(y.first < x.first) && x.second < y.second); 
}

template<class T1, class T2>
inline pair<T1, T2> make_pair(const T1& x, const T2& y) {
    return pair<T1, T2>(x, y);
}

Open in new window

I get the following errors when compiling in Unix:

line 33: Error: The name pair is ambiguous, pair<T1, T2> and std::pair<std::_T1, std::_T2>.
line 38: Error: The name pair is ambiguous, pair<T1, T2> and std::pair<std::_T1, std::_T2>.
line 43: Error: The name pair is ambiguous, pair<T1, T2> and std::pair<std::_T1, std::_T2>.
Angela StevensonSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>line 33: Error

Now that's interesting, because the above code is only 32 lines...
0
Angela StevensonSoftware DeveloperAuthor Commented:
Sorry I deleted the comments so it would take up as much space.
0
jkrCommented:
Well, see yourself, it's still not enough lines ;o)

But I guess I see the problem. Try to explicitly distinguish your 'pair' from 'std::pair' by using

#ifndef PAIR_H
#define PAIR_H
#endif

#include <bool.h>

template<class T1, class T2>
struct pair {
    T1 first;
    T2 second;
    pair() : first(T1()), second(T2()) {}
    pair(const T1& a, const T2& b) : first(a), second(b) {}
    friend void destroy(pair<T1, T2>* p) {
//Ed Y.      p->~pair();
    }
};

template<class T1, class T2>
inline iBOOL operator==(const ::pair<T1, T2>& x, const ::pair<T1, T2>& y) { 
    return x.first == y.first && x.second == y.second; 
}

template<class T1, class T2>
inline iBOOL operator<(const ::pair<T1, T2>& x, const ::pair<T1, T2>& y) { 
    return x.first < y.first || (!(y.first < x.first) && x.second < y.second); 
}

template<class T1, class T2>
inline ::pair<T1, T2> make_pair(const T1& x, const T2& y) {
    return pair<T1, T2>(x, y);
}
                   

Open in new window


Or introduce your own namespace, e.g. like

#ifndef PAIR_H
#define PAIR_H
#endif

#include <bool.h>

namespace MyScope

template<class T1, class T2>
struct pair {
    T1 first;
    T2 second;
    pair() : first(T1()), second(T2()) {}
    pair(const T1& a, const T2& b) : first(a), second(b) {}
    friend void destroy(pair<T1, T2>* p) {
//Ed Y.      p->~pair();
    }
};

template<class T1, class T2>
inline iBOOL operator==(const pair<T1, T2>& x, const pair<T1, T2>& y) { 
    return x.first == y.first && x.second == y.second; 
}

template<class T1, class T2>
inline iBOOL operator<(const pair<T1, T2>& x, const pair<T1, T2>& y) { 
    return x.first < y.first || (!(y.first < x.first) && x.second < y.second); 
}

template<class T1, class T2>
inline pair<T1, T2> make_pair(const T1& x, const T2& y) {
    return pair<T1, T2>(x, y);
}

} // end MyScope
                        

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peprCommented:
That is probably because you included std::pair<> somehow (possibly indirectly), and you did something like using namespace std;. This way, you definitions of pair<> is similar (with respect to decide which one should be used). Notice that the error message explicitly states std::...

Remove the using namespace std; Or rename the pair to my_pair or so.

Despite what puritsts say about never using it, the using namespace std; can almost always be used in non-shared-library projects (and I often use it). However, this case is exactly when it cannot be used.
0
peprCommented:
Oh my! 13 seconds faster :)))

Oh no, not again!

http://www.youtube.com/watch?feature=player_detailpage&v=MsK6aRuSBIc#t=134
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.