当我编译我的程序时,我收到以下错误消息:
stringPerm.cpp: In function ‘int main()’:
stringPerm.cpp:12: error: expected primary-expression before ‘word’
我尝试在将变量分配给函数之前在单独的行上定义变量,但最终得到了相同的错误消息。
原因是什么?如何解决该问题?
这是代码:
#include <iostream>
#include <string>
using namespace std;
string userInput();
int wordLengthFunction(string word);
int permutation(int wordLength);
int main()
{
string word = userInput();
int wordLength = wordLengthFunction(string word);
cout << word << " has " << permutation(wordLength) << " permutations." << endl;
return 0;
}
string userInput()
{
string word;
cout << "Please enter a word: ";
cin >> word;
return word;
}
int wordLengthFunction(string word)
{
int wordLength;
wordLength = word.length();
return wordLength;
}
int permutation(int wordLength)
{
if (wordLength == 1)
{
return wordLength;
}
else
{
return wordLength * permutation(wordLength - 1);
}
}
您的调用中不需要“字符串”wordLengthFunction()
.
int wordLength = wordLengthFunction(string word);
应该
int wordLength = wordLengthFunction(word);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)