void perm(std::string fixed,std::string perm){
if (perm.length() == 1){
std::cout << fixed perm << std::endl;
}else{
for (int i=0;i<perm.length();i ){
std::string perm2 = perm;
std::swap(perm2[0],perm2[i]);
perm(fixed perm[i],perm2.substr(1,perm.length()-1));
}
}
}
This is my first question and I am (very) new to programming. I was working on a mini program which simply prints all the permutations of a string. For example, perm("","123") would be supposed to return the following values
123
132
213
231
312
321
However, on compilation, the g compiler returns the following error:
permutations.cpp: In function ‘void perm(std::string, std::string)’:
permutations.cpp:19:29: error: no match for call to ‘(std::string {aka
std::__cxx11::basic_string<char>}) (std::__cxx11::basic_string<char>,
std::__cxx11::basic_string<char>)’
19 | perm(fixed perm[i],perm2.substr(1,perm.length()-1));
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I would like to know the cause of this error and troubleshoot it. Thank you.
CodePudding user response:
void perm(std::string fixed,std::string perm){
You have a function called perm, and one of its parameters is also called perm, same name. Danger ahead.
perm(fixed perm[i],perm2.substr(1,perm.length()-1));
According to rules of C , the first perm here is a std::string object, one of the parameters to this function. This is not getting parsed as a recursive call to the same function, but an attempt to invoke a suitable operator() on a std::string object. Your C compiler is grumpy because no suitable overload was found.
Solution: don't use the same name for a function and one of its parameters.
CodePudding user response:
The quick fix would be to specify that you want the function perm, not the std::string instance with the same name.
::perm(fixed perm[i], perm2.substr(1, perm.length() - 1));
// ^^
But I suggest that you change the name of one of them.
