--------------------------------------------------------------------------------

I am trying to do a program that will use a function to search a string for an occurence of another string. Can someone please tell me what is wrong with this?

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

void replaceSubstring(string, string, string);

int main(){
string string1, string2, string3;
cout <<"Substring Program!\n\n\n";
cout << "Please enter string one: \n";
getline (cin, string1);
cout << "Please enter string two: \n";
getline (cin, string2);
cout << "Please enter string three: \n";
getline (cin, string3);
replaceSubstring(string1, string2, string3);
system ("pause");
return 0;
}

void replaceSubstring(string string1, string string2, string string3){
char* a=NULL;
a=strstr(string1, string2);
cout << a;
}

I get this error:
"no matching function for call to `strstr(std::string&, std::string&)' "

Recommended Answers

All 3 Replies

You can tell because it is looking for a function that takes two arguments, both (std::string&)s.
However, the documentation for strstr() says that each argument must be a (char *).

Fix it by extracting the (char *)s from the (std::string)s: a=strstr(string1.c_str(), string2.c_str()); There is, BTW, a C++ way to do this: int index = string1.find( string2, 0 ); This way is better (and safer).

Also, don't use system("PAUSE") if you can avoid it. Look here.

Hope this helps.

thank you for the help! i've got it now.

You can tell because it is looking for a function that takes two arguments, both (std::string&)s.
However, the documentation for strstr() says that each argument must be a (char *).

Fix it by extracting the (char *)s from the (std::string)s: a=strstr(string1.c_str(), string2.c_str()); There is, BTW, a C++ way to do this: int index = string1.find( string2, 0 ); This way is better (and safer).

Also, don't use system("PAUSE") if you can avoid it. Look here.

Hope this helps.

got it! thanks a lot for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.