anon
anon

Reputation:

g++ error: ‘stricmp’ was not declared in this scope (but OK for 'strcmp')

I am trying to compile the following very very simple piece of source code:

#include <cstring>
// #include <string.h>
// using namespace std;

class Helper {
public:
    int cStringsAreEqual(const char *s1, const char *s2) {
        return stricmp(s1, s2);
    }
};

... but I am getting the following error message:

   g++ error: ‘stricmp’ was not declared in this scope

However when I use strcmp() instead of stricmp() then everything is fine!

What can be wrong here? Shouldn't stricmp() be allowed when strcmp() is allowed?

Sureley, this all could be written in a much better way without using strcmp/stricmp.

But that's not the point here.

I am porting a piece of software - which makes much use of calls to stricmp(). And if somehow possible I would like to avoid all of the efforts needed to change every call to stricmp.

Any help on this would be very much appreciated!

BTW: I am using Ubuntu karmic OS (v9.10) with g++ v4.4.1.

BTW: as you can see I also made some trials with '#include string.h' or with 'namespace std' but nothing helped.

Upvotes: 31

Views: 74988

Answers (5)

Matt Chambers
Matt Chambers

Reputation: 2346

If you've got Boost, use boost::algorithm::iequals(s1, s2, std::locale::classic()) in <boost/algorithm/string/predicate.hpp> (or leave off locale if you want locale-sensitivity). It works with C strings, std::[w]string, vector<char>, etc.

Upvotes: -1

Gary
Gary

Reputation: 1

Pretty easy to make your own if need be...

int my_stricmp (const char *s1, const char *s2)
{
    while (*s1 != 0 && *s2 != 0)
    {
        if (*s1 != *s2 && ::toupper (*s1) != ::toupper(*s2))
        {
            return -1;
        }
        s1++;
        s2++;
    }
    return (*s1 == 0 && *s2 == 0) ? 0 : -1;
}

Upvotes: -3

Ryan Christensen
Ryan Christensen

Reputation: 7933

Add a define for it to overwrite stricmp with strcasecmp on the platforms you are looking for.

#ifdef _IPHONE <- your platform define here
#define stricmp strcasecmp
#define strnicmp strncasecmp
#endif

Then you can just use stricmp always.

Upvotes: 14

Gonzalo
Gonzalo

Reputation: 21175

Try strcasecmp(). Here's the manual page for it. It is conforming to 4.4BSD and POSIX.1-2001.

Upvotes: 46

Mark Rushakoff
Mark Rushakoff

Reputation: 258208

stricmp is neither POSIX nor ANSI, so it doesn't really matter if strcmp is allowed, if your compiler or standard library is strictly adhering to POSIX or ANSI standard library functions (as is probably the case with the GCC suite).

Upvotes: 14

Related Questions