gpt4 book ai didi

C++ -- ' ' 之前的预期主表达式

转载 作者:IT老高 更新时间:2023-10-28 21:38:31 32 4
gpt4 key购买 nike

我是 C++ 和编程新手,遇到了一个我无法弄清楚的错误。当我尝试运行该程序时,我收到以下错误消息:

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);

关于C++ -- ' ' 之前的预期主表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11507607/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com