为什么我的字符串未定义?
Why is my string undefined?
错误提示 s
在我的代码中此时未定义:
cout << "Enter the string : ";
cin >> s;
我该如何解决?
此外,我的第二个括号中的另一个错误是 "expecting a ;"。我该怎么做才能解决这个问题?
这是我的完整代码:
#include <stdafx.h>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main()
{
void permutation(string s, int i, int n)
{
int j;
if (i == n)
cout << s << "\t";
else
{
for (j = i; j < s.length(); j++)
{
swap(s[i], s[j]);
permutation(s, i + 1, n);
swap(s[i], s[j]);
cout << "Enter the string : ";
cin >> s;
cout << endl << "The permutations of the given string : " << endl;
permutation(s, 0, s.length() - 1);
cout << endl;
}
我没看到你定义字符串s的地方。 C++ 是声明式的。您必须在使用它们之前声明所有变量。它不同于 PHP,例如,初始化一个变量等同于声明它。
再看看括号的嵌套。看起来您在 main() 中声明了置换函数。这可能会使编译器感到困惑。
void permutation(string s, int i, int n) 在 main 中定义。将它放在外面并检查。以下是错误的
int main()
{
void permutation(string s, int i, int n)
您在主块中声明了您的函数,这导致了编译错误。在外面声明为-
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
void permutation(string s, int i, int n)
{
int j;
if (i == n)
cout << s << "\t";
else
{
for (j = i; j < s.length(); j++)
{
swap(s[i], s[j]);
permutation(s, i + 1, n);
swap(s[i], s[j]);
}
}
}
int main()
{
string s;
cout << "Enter the string : ";
cin >> s;
cout << endl << "The permutations of the given string : " << endl;
permutation(s, 0, s.length() - 1);
cout << endl;
}
错误提示 s
在我的代码中此时未定义:
cout << "Enter the string : ";
cin >> s;
我该如何解决?
此外,我的第二个括号中的另一个错误是 "expecting a ;"。我该怎么做才能解决这个问题?
这是我的完整代码:
#include <stdafx.h>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main()
{
void permutation(string s, int i, int n)
{
int j;
if (i == n)
cout << s << "\t";
else
{
for (j = i; j < s.length(); j++)
{
swap(s[i], s[j]);
permutation(s, i + 1, n);
swap(s[i], s[j]);
cout << "Enter the string : ";
cin >> s;
cout << endl << "The permutations of the given string : " << endl;
permutation(s, 0, s.length() - 1);
cout << endl;
}
我没看到你定义字符串s的地方。 C++ 是声明式的。您必须在使用它们之前声明所有变量。它不同于 PHP,例如,初始化一个变量等同于声明它。
再看看括号的嵌套。看起来您在 main() 中声明了置换函数。这可能会使编译器感到困惑。
void permutation(string s, int i, int n) 在 main 中定义。将它放在外面并检查。以下是错误的
int main()
{
void permutation(string s, int i, int n)
您在主块中声明了您的函数,这导致了编译错误。在外面声明为-
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
void permutation(string s, int i, int n)
{
int j;
if (i == n)
cout << s << "\t";
else
{
for (j = i; j < s.length(); j++)
{
swap(s[i], s[j]);
permutation(s, i + 1, n);
swap(s[i], s[j]);
}
}
}
int main()
{
string s;
cout << "Enter the string : ";
cin >> s;
cout << endl << "The permutations of the given string : " << endl;
permutation(s, 0, s.length() - 1);
cout << endl;
}