i have following code
#include <iostream>
#include <string>
using namespace std;
string generate(){
for (char c1='A';c1<='Z';c1++){
for (char c2='A';c2 <='Z';c2++){
for (char c3='A';c3<='Z';c3++){
for (char c4='A';c4<='Z';c4++){
return (new string *)(c1) + (new string*)(c2)+(new string*)(c3)+(new string*)(c4);
}
}
}
}
}
int main(){
return 0;
}
i want to generate strings but here is error
1>------ Build started: Project: string_combinations, Configuration: Debug Win32 ------
1>Build started 9/11/2010 12:42:08 PM.
1>InitializeBuildStatus:
1> Touching "Debug\string_combinations.unsuccessfulbuild".
1>ClCompile:
1> string_combinations.cpp
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments
1>c:\users\david\documents\visual studio 2010\projects\string_combinations\string_combinations\string_combinations.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.82
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
please help i am confused why i can’t directly convert from char to string by this method string(char)
The problem is with your expressions of this form:
The left hand side isn’t a type, it’s an expression. When you suffix it with another parenthesized expression it looks like a function call but that only works if the left expression is a function name or function pointer. In this case the new expression has type
std::string**which isn’t a function pointer.To construct a temporary string from a single
char, you shouldn’t usenewwhich dynamically allocates an object; instead you can use a constructor. A suitable one is the one which takes a count and acharto repeat for that count. In your case a count of 1 is what you want:You can do something like.
Note that you also don’t call generate anywhere and if you do
returnfrom the first iteration of aforloop you aren’t going to be iterating through all the combinations, you will only every generate the first compination.