Ambiguous Overloading?

Hey, I've got the following function coded:

[code 221]
bool invalidChar(const char *string)
{
//Check for \ / : * ? < > |
char invalid[] = "\\/:*?<>|";
char *pch;

pch = strpbrk(*string, invalid);

if (pch == NULL)
return false;
else
return true;
}
[/code]

but whenever I try to build I get the following error:

1
2
3
..\src\Renamer.cpp:221: error: call of overloaded `strpbrk(const char&, char[9])' is ambiguous
C:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/string.h:53: note: candidates are: char* strpbrk(const char*, const char*) <near match>
C:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/cstring:113: note:                 char* std::strpbrk(char*, const char*) <near match> 


Any ideas?
Maybe you should not use 'string' as the string name. Try MyString or something like that.

int main
I tried that, still the same error.
I'm still getting the hang of pointers. but..

Just give this a shot..

pch = strpbrk(string, invalid);

Notice, i dropped the derefferencer '*' from the string. (it may not hurt to use mystring or some other name anyway, even though you have determined that the name wasn't the problem)

I'm not sure if it'll fix it, but it's worth a shot.
The worse that'll happen is it'll give the same or even a new error. which can be fixed by putting the derefferencer back in.
Last edited on
closed account (z05DSL3A)
Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>

bool invalidChar(const char *str)
  {
    //Check for \ / : * ? < > |
    char invalid[] = "\\/:*?<>|";
    char *pch;
    
    pch = strpbrk((char *)str, invalid);
    
    if (pch == NULL)
      return false;
    else
      return true;
  }

 

int main()
{
    char str[] = "This is a sample string?";
    if (invalidChar(str))
        std::cout << "Has invalid Char." << std::endl;
    else
        std::cout << "OK" << std::endl;

    return 0;
}
Last edited on
Thanks Grey Wolf. That solved it. Question: WHY did it solve it?
Last edited on
closed account (z05DSL3A)
The compiler had a choice of:
char* strpbrk(const char*, const char*)
or
char* std::strpbrk(char*, const char*)
by casting str to a char *, the compiler can then see that you want to use the second function.
Ah, thanks again!
I was just wondering why you are using the C string library in a C++ program. Nearly all of the str... functions in C are deprecated in C++.

You should be using a string object.

Topic archived. No new replies allowed.