2012-07-16 115 views
11

Actualización: ¡Gracias a todos por sus rápidas respuestas - problema resuelto!C++ - expresión primaria esperada antes de ''

Soy nuevo en C++ y la programación, y he encontrado un error que no puedo entender. Cuando trato de ejecutar el programa, aparece el siguiente mensaje de error:

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

También he intentado definir las variables en una línea separada antes de asignarlos a las funciones, pero me acaban de recibir el mismo mensaje de error .

¿Alguien puede dar algunos consejos al respecto? ¡Gracias por adelantado!

Ver código de abajo:

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

'int longitud de palabra = wordLengthFunction (palabra);' –

Respuesta

16

No es necesario "cadena" en su llamada a wordLengthFunction().

int wordLength = wordLengthFunction(string word);

debería ser

int wordLength = wordLengthFunction(word);

+0

Genial, gracias por tu ayuda! – LTK

7

Cambio

int wordLength = wordLengthFunction(string word); 

a

int wordLength = wordLengthFunction(word); 
+0

Grandes, gracias por su ayuda! – LTK

5

Usted no debe estar repitiendo la parte string al enviar parámetros.

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

Genial, gracias por tu ayuda! – LTK

Cuestiones relacionadas