Need Help with Standard Library Wierdness

Need Help with Standard Library Wierdness

Post by Chris Gehlk » Mon, 24 May 1999 04:00:00



I'm getting a wierd error trying to compile a substring search. I'm doing:

string::const_iterator i = find(range.begin(), range.end(), target);

and the compiler is barfing in:
...:Metrowerks Standard Library:MSL C++:Include:algorithm

It doesn't like the routine:

template <class InputIterator, class T>
inline
InputIterator
find(InputIterator first, InputIterator last, const T& value)
{
   while (first != last && !(*first == value))
      ++first;
   return first;

Quote:}

It says the penultimate ")" in the while line is an illegal operand.

It may be relevant that range is passed the the searching routine as
"const string&" so the compiler is barfing on taking a pointer to a const
but I thinks what I'm doing is pretty standard. If I change my declaration
to:

const string::const_iterator i

it doesn't break in the library file but it does tell me any later use of
find is an illegal attempt to modify a const.

 
 
 

Need Help with Standard Library Wierdness

Post by Howard Hinna » Mon, 24 May 1999 04:00:00




> I'm getting a wierd error trying to compile a substring search. I'm doing:

> string::const_iterator i = find(range.begin(), range.end(), target);

> and the compiler is barfing in:
> ...:Metrowerks Standard Library:MSL C++:Include:algorithm

> It doesn't like the routine:

> template <class InputIterator, class T>
> inline
> InputIterator
> find(InputIterator first, InputIterator last, const T& value)
> {
>    while (first != last && !(*first == value))
>       ++first;
>    return first;
> }

> It says the penultimate ")" in the while line is an illegal operand.

> It may be relevant that range is passed the the searching routine as
> "const string&" so the compiler is barfing on taking a pointer to a const
> but I thinks what I'm doing is pretty standard. If I change my declaration
> to:

> const string::const_iterator i

> it doesn't break in the library file but it does tell me any later use of
> find is an illegal attempt to modify a const.

I'm having trouble duplicating your results.  This works for me:

#include <string>
#include <algorithm>

using namespace std;

bool
has_one(const string& range, char target)
{
   string::const_iterator i = find(range.begin(), range.end(), target);
   return i != range.end();

Quote:}

int main()
{
   string test("Hi");
   bool b = has_one(test, 'H');

Quote:}

If you can post a complete demo, I'll try to pinpoint the problem.  Also,
in case it's handy, string has a fairly complete set of member find
functions:

   size_type find (const basic_string& str, size_type pos = 0) const;
   size_type find (const charT* s, size_type pos, size_type n) const;
   size_type find (const charT* s, size_type pos = 0) const;
   size_type find (charT c, size_type pos = 0) const;
   size_type rfind(const basic_string& str, size_type pos = npos) const;
   size_type rfind(const charT* s, size_type pos, size_type n) const;
   size_type rfind(const charT* s, size_type pos = npos) const;
   size_type rfind(charT c, size_type pos = npos) const;
   size_type find_first_of(const basic_string& str, size_type pos = 0) const;
   size_type find_first_of(const charT* s, size_type pos, size_type n) const;
   size_type find_first_of(const charT* s, size_type pos = 0) const;
   size_type find_first_of(charT c, size_type pos = 0) const;
   size_type find_last_of(const basic_string& str, size_type pos = npos) const;
   size_type find_last_of(const charT* s, size_type pos, size_type n) const;
   size_type find_last_of(const charT* s, size_type pos = npos) const;
   size_type find_last_of(charT c, size_type pos = npos) const;
   size_type find_first_not_of(const basic_string& str, size_type pos = 0)
const;
   size_type find_first_not_of(const charT* s, size_type pos, size_type n)
const;
   size_type find_first_not_of(const charT* s, size_type pos = 0) const;
   size_type find_first_not_of(charT c, size_type pos = 0) const;
   size_type find_last_not_of (const basic_string& str, size_type pos =
npos) const;
   size_type find_last_not_of (const charT* s, size_type pos, size_type n)
const;
   size_type find_last_not_of (const charT* s, size_type pos = npos) const;
   size_type find_last_not_of (charT c, size_type pos = npos) const;

-Howard

 
 
 

Need Help with Standard Library Wierdness

Post by Darin Adle » Mon, 24 May 1999 04:00:00


Quote:>    string::const_iterator i = find(range.begin(), range.end(), target);

> the compiler is barfing in:

>    template <class InputIterator, class T> inline InputIterator
>    find(InputIterator first, InputIterator last, const T& value)
>    {
>        while (first != last && !(*first == value))
>            ++first;
>        return first;
>    }

> It says the penultimate ")" in the while line is an illegal operand.

The illegal operand is the parameter you are passing for value.

The problem is that the function you are using, std::find, can only search
for a single element in the iterated sequence. Thus, target must be a single
character, since string is a collection of characters. From your message and
the error you are getting, I'd guess that your target is a std::string or a
const char* rather than a char, although you didn't say so.

If you want to search for a multiple-character sequence, you'll need to use
search instead. If target is a std::string:

    string::const_iterator i = search(range.begin(), range.end(),
        target.begin(), target.end());

If target is a const char*:

    string::const_iterator i = search(range.begin(), range.end(),
        target, target + strlen(target));

Or you could use std::string's find member function rather than using a
standard algorithm. Since that function returns a position within the
string, if you want to get the result in the form of an iterator you'll have
to convert the result, which is a little tricky:

    int j = range.find(target);
    string::const_iterator i = (j == string::npos)
        ? range.end() : (range.begin() + j);

    -- Darin

 
 
 

Need Help with Standard Library Wierdness

Post by Chris Gehlk » Mon, 24 May 1999 04:00:00






> > I'm getting a wierd error trying to compile a substring search. I'm doing:

> > string::const_iterator i = find(range.begin(), range.end(), target);

[snip]

Quote:

> > it doesn't break in the library file but it does tell me any later use of
> > find is an illegal attempt to modify a const.

> I'm having trouble duplicating your results.  This works for me:

> #include <string>
> #include <algorithm>

> using namespace std;

> bool
> has_one(const string& range, char target)
> {
>    string::const_iterator i = find(range.begin(), range.end(), target);
>    return i != range.end();
> }

> int main()
> {
>    string test("Hi");
>    bool b = has_one(test, 'H');
> }

Thanks Howard,
It was easist to duplicate the problem with a slight modification of your
code. char interators work but substring iterators won't compile.

#include <string>
#include <algorithm>

using namespace std;

bool
has_one(const string& range, char target); // This works

bool
has_one(const string& range, const string& target); // This won't

bool
has_one(const string& range, char target)
{
   string::const_iterator i = find(range.begin(), range.end(), target);
   return i != range.end();

Quote:}

bool
has_one(const string& range, const string& target)
{
   string::const_iterator i = find(range.begin(), range.end(), target);
   return i != range.end();

Quote:}

int main()
{
   string test("Hi");
   bool b = has_one(test, 'H');

   string test2("Aardvark");
   bool a = has_one(test2, "Aa");

- Show quoted text -

Quote:}

 
 
 

Need Help with Standard Library Wierdness

Post by Chris Gehlk » Mon, 24 May 1999 04:00:00




> The problem is that the function you are using, std::find, can only search
> for a single element in the iterated sequence. Thus, target must be a single
> character, since string is a collection of characters. From your message and
> the error you are getting, I'd guess that your target is a std::string or a
> const char* rather than a char, although you didn't say so.

> If you want to search for a multiple-character sequence, you'll need to use
> search instead. If target is a std::string:

>     string::const_iterator i = search(range.begin(), range.end(),
>         target.begin(), target.end());

That is exactly right, Darin. Ididn't know the difference between find()
and search(). Thanks.
 
 
 

1. PP and shared library wierdness

We're working on a project that requires UI being implemented in a
shared library.

The arcitecture is as follows:

* All PP classes have been compiled into their own shared library.
* There is a set of API calls that will live in another shared library.
* The calling app will link against these and make calls into the APIs

I am bringing up an LDialogBox with StDialogHandler() in the API lib.
The problem is that I can only get the window to open once. If I close
it and reopen it, I get a consistant crash.  It seems to die while
building object stream(s) called ultimately by LWindow::CreateWindow().

It usually dies at the following case [in ObjectsFromStream()]:

   case tag_BeginSubs:
        currentObject = ObjectsFromStream(inStream);
    break;

OK...clearly its reentrant code so my first suspicion was a stack/heap
collision, but inspection of available heap and stack preclude a
collision...at least by the time I get to inspect it. Also, the heap is
way bigger than it needs to be and there's plenty of free space.

BTW, if I compile the API library source code directly into the App
project [still linking with the PP shared lib] the problem disappears.

Any suggestions for resolution or further diagnostics/debugging tricks
would be appreciated.

TIA.

2. Terminal service question.

3. Shared libraries, code resources HELP needed !

4. Importing Netscape address book into IMN

5. FAT Shared library: help needed

6. Cable modem/DSL - choice

7. Need Help chosing system Libs to include in Shared Library

8. Where to find bstat ?

9. Need help with MSL C++ library

10. Need help finding libraries for an old Pascal project

11. Need help with a library in Pro 1

12. HELP - HTTP source code/library needed

13. need info on carbon compatibility of standard libs