Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C++ Namespace Problem

Posted on 2006-06-09
7
502 Views
Last Modified: 2012-06-27
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

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

0
Comment
Question by:huffmana
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 16870051
template <>
struct std::less<MenuID> // <--- make that a member of 'std'
  : 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());
                }
        }
};
0
 

Author Comment

by:huffmana
ID: 16870245
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 16870296
>>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)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:huffmana
ID: 16870480
I'm trying, right now I'm getting inconsistant results....  I'll get back to you in a few minutes.
Merci Beaucoup :-)  Allan
0
 

Author Comment

by:huffmana
ID: 16871278
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 16871420
>>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 {

//...

};
0
 

Author Comment

by:huffmana
ID: 16871448
Oh...  Many Thanks.  Allan
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 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