Solved

C++ Namespace Problem

Posted on 2006-06-09
7
505 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path to  STL Map header file 1 91
How can i compile this github project?? 2 104
HTTPSendRequest with WinINet delays on first call 11 120
No module found pypyodbc, 3 58
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

752 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