C++ verwachte primaire expressie vóór ‘ ‘

Ik ben nieuw in C++ en programmeren, en ben een fout tegengekomen die ik niet kan achterhalen. Wanneer ik het programma probeer uit te voeren, krijg ik de volgende foutmelding:

stringPerm.cpp: In function ‘int main()’:
stringPerm.cpp:12: error: expected primary-expression before ‘word’

Ik heb ook geprobeerd de variabelen op een aparte regel te definiëren voordat ik ze aan de functies toewijs, maar ik krijg uiteindelijk dezelfde foutmelding.

Kan iemand hier advies over geven? Bij voorbaat dank!

Zie onderstaande code:

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

Antwoord 1, autoriteit 100%

Je hebt geen “string” nodig in je aanroep naar wordLengthFunction().

int wordLength = wordLengthFunction(string word);

zou moeten zijn

int wordLength = wordLengthFunction(word);


Antwoord 2, autoriteit 33%

Wijzigen

int wordLength = wordLengthFunction(string word);

naar

int wordLength = wordLengthFunction(word);

Antwoord 3, autoriteit 21%

Je moet het gedeelte string niet herhalen bij het verzenden van parameters.

int wordLength = wordLengthFunction(word); //you do not put string word here.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

thirteen − eleven =

Other episodes