23

我是 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);
    }    
}
4

3 回答 3

25

在调用wordLengthFunction().

int wordLength = wordLengthFunction(string word);

应该

int wordLength = wordLengthFunction(word);

于 2012-07-16T15:39:10.577 回答
8

改变

int wordLength = wordLengthFunction(string word);

int wordLength = wordLengthFunction(word);
于 2012-07-16T15:39:03.470 回答
5

string发送参数时不应重复该部分。

int wordLength = wordLengthFunction(word); //you do not put string word here.
于 2012-07-16T15:39:19.650 回答