[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unknown scope in pair.h

Posted on 2014-08-18
7
Medium Priority
?
81 Views
Last Modified: 2015-05-19
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>.
0
Comment
Question by:Angela Stevenson
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40268212
>>line 33: Error

Now that's interesting, because the above code is only 32 lines...
0
 

Author Comment

by:Angela Stevenson
ID: 40268309
Sorry I deleted the comments so it would take up as much space.
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 40268330
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
 
LVL 29

Assisted Solution

by:pepr
pepr earned 1000 total points
ID: 40268331
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
 
LVL 29

Expert Comment

by:pepr
ID: 40268350
Oh my! 13 seconds faster :)))

Oh no, not again!

http://www.youtube.com/watch?feature=player_detailpage&v=MsK6aRuSBIc#t=134
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question