Link to home
Start Free TrialLog in
Avatar of huffmana
huffmanaFlag for United States of America

asked on

C++ Namespace Problem

Using the new gcc namespace is causing me a problem with a struct declaration.  I would like to use a full name for struct but I don’t know what it is.

Line 14 in less_MenuID.h is this statement "  : public binary_function<MenuID, MenuID, bool>" as seen below below.

Thanks for your help,  Allan
===========Compile Error===========================

# make
g++ -g -Wall -I../util -c RemoveCycles.cc
In file included from RemoveCycles.h:10,
                 from RemoveCycles.cc:2:
less_MenuID.h:14: error: specialization of `template<class _Tp> struct std::less' in different namespace
/usr/local/lib/gcc/sparc-sun-solaris2.8/3.4.2/../../../../include/c++/3.4.2/bits/stl_function.h:223: error:   from definition of `template<class _Tp> struct std::less'
make: *** [RemoveCycles.o] Error 1
# pwd


===========less_MenuID.h==========================
#ifndef LESS_MENUID_H
#define LESS_MENUID_H

#include <set>
#include <algorithm>
/*using namespace std;    // added Allan */
#include "MenuID.h"

/*
 * class for arbitrary ordering in a set<MenuID>
 */
template <>
struct less<MenuID>
  : public binary_function<MenuID, MenuID, bool>
{
        bool operator()(const MenuID & mid1, const MenuID & mid2) const
        {
                if (mid1.getSegmentName() == mid2.getSegmentName())
                {
                        if (mid1.getType() == mid2.getType())
                        {
                                return (mid1.getMenuName() < mid2.getMenuName());
                        }
                        else
                        {
                                return (mid1.getType() < mid2.getType());
                        }
                }
                else
                {
                        return (mid1.getSegmentName() < mid2.getSegmentName());
                }
        }
};

#endif

===========================================================

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of huffmana

ASKER

I've tried to limit the std namespace by using "using std::string;" ...

Your change to std::less worked great :-)  Thanks

But now even public is "in different namespace" !!  Should I do the following?  Or go back to "using namespace std;"  ?

std::public binary_function<MenuID, MenuID, bool>

Is there a good URL where I can read up about the new std namespace?

Thank, Allan
>>Should I do the following?  Or go back to "using namespace std;"  ?
>>std::public binary_function<MenuID, MenuID, bool>

That should be

template <>
struct std::less<MenuID> // <--- make that a member of 'std'
  : public std::binary_function<MenuID, MenuID, bool> // <--- directly reference it
{
//...
}

I'd say you can use 'usinag namespace std;' except if that is in a header file (could lead to ambiguities)
I'm trying, right now I'm getting inconsistant results....  I'll get back to you in a few minutes.
Merci Beaucoup :-)  Allan
I don't understand.  With your instruction, I had passed this error:
less_MenuID.h:13: error: specialization of `template<class _Tp> struct std::less' in different namespace
=======================TRIAL 1=========================
I went back and changed all the
    "using std::string;" ==> to ==> "using namespace std;"
every where there is an "#include <string>" like the following.

and a compiler gives this error:
less_MenuID.h:13: error: specialization of `template<class _Tp> struct std::less' in different namespace
=======================TRIAL 2=========================
Now I've changed all
    "using namespace std;" ==> to ==> "using std::string;"
and added:
    #include <vector>
    using std::vector;       // added Allan
    #include <iostream>
    using std::iostream;       // added Allan

But iostream does not like this so I've had to prefix all iostream references with:
            printMenus.h:13: error: ISO C++ forbids declaration of `ostream' with no type

    // printMenus(ostream & o);
    printMenus(std::ostream & o);       // added Allan:

and things are compiling now....

=======================TRIAL 3=========================
Now I should try putting "using std::iostream;" only in the .cc programs and not in the headers.....

---------------------------------------------------------------------------------------------------
Your help got me here !!!  Thanks very much.  I'll close this out in 20 minutes in case you wanted to reply.
Thanks Again, Allan
>>I went back and changed all the
>>    "using std::string;" ==> to ==> "using namespace std;"

No, if you want to put something *into* std, you'll have to use

namespace std {

//...

};
Oh...  Many Thanks.  Allan