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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11507607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:12:23  来源:igfitidea点击:

C++ -- expected primary-expression before ' '

c++stringfunctionvariables

提问by LTK

Update: Thanks everyone for your quick responses -- problem solved!

更新:感谢大家的快速回复——问题已解决!

I am new to C++ and programming, and have run into an error that I cannot figure out. When I try to run the program, I get the following error message:

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

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

I've also tried defining the variables on a separate line before assigning them to the functions, but I end up getting the same error message.

我还尝试在将变量分配给函数之前在单独的行上定义变量,但最终得到相同的错误消息。

Can anyone offer some advice about this? Thanks in advance!

任何人都可以就此提供一些建议吗?提前致谢!

See code below:

见下面的代码:

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

回答by Omaha

You don't need "string" in your call to wordLengthFunction().

您在调用wordLengthFunction().

int wordLength = wordLengthFunction(string word);

int wordLength = wordLengthFunction(string word);

should be

应该

int wordLength = wordLengthFunction(word);

int wordLength = wordLengthFunction(word);

回答by fbafelipe

Change

改变

int wordLength = wordLengthFunction(string word);

to

int wordLength = wordLengthFunction(word);

回答by crashmstr

You should not be repeating the stringpart when sending parameters.

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

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