Solved

C++ Namespace Problem

Posted on 2006-06-09
7
496 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now