?
Solved

C++ Namespace Problem

Posted on 2006-06-09
7
Medium Priority
?
507 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
[X]
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
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

718 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