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 に答える