【发布时间】:2015-06-18 03:24:04
【问题描述】:
错误提示 s 在我的代码中此时未定义:
cout << "Enter the string : ";
cin >> s;
我该如何解决这个问题?
另外,我的第二个括号中的另一个错误是“期望一个;”。我能做些什么来解决这个问题?
这是我的完整代码:
#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;
}
【问题讨论】:
-
先修复first错误。您似乎正试图在您的
main函数中声明一个新函数 (permutation)。这是您需要解决的第一件事。否则,编译器在看到第二个{时会感到非常困惑,因此您无法相信它在此之后发现的任何错误。 -
你的代码完全崩溃了,为什么
void permutation(string s, int i, int n)在main中? -
代码甚至无法编译.....
-
main()不表示模块的主要范围区域。在 C/C++ 中,main()是一个入口点函数。如果你想定义一个函数,就在外面做。
标签: c++ permutation