gpt4 book ai didi

c++ - 在 Visual Studio 2015 中调用函数错误

转载 作者:行者123 更新时间:2023-11-28 05:26:00 25 4
gpt4 key购买 nike

更新:好的,谢谢大家!我的教科书说函数原型(prototype)要求在括号中声明函数,并且显示的示例看起来与函数头和函数体中的代码本身略有不同,所以..我按照它向我展示的内容进行了操作。我根据将原型(prototype)的括号留空的建议对其进行了更正,它起作用了。

郑重声明,我真的很讨厌这些教科书...再次感谢大家的帮助!

操作:我收到此错误:

C2660 'getLetter': function does not take 0 arguments.

代码是这样的:

#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;

char getLetter(char letter);

int main()
{
char firstLetter = getLetter();
cout << firstLetter;
return 0;
} //end of main function

char getLetter()
{
char letter = ' ';
cout << "Enter a character: ";
cin >> letter;
return letter;
} // end of getLetter function

我的代码看起来像书上给出的例子,但是没有一个例子使用“char”函数,它们都使用double或int;不确定这是否重要。错误(在第 12 行,主函数的底部)也是我的导师编写的代码,而不是我,这让我更加困惑。我在掌握这一课时遇到困难,需要另一种观点。

最佳答案

在以下代码片段中调用函数之前

int main()
{
char firstLetter = getLetter();
^^^^^^^^^^
//...

您将名称 getLetter 声明为具有一个参数的函数

char getLetter(char letter);
^^^^^^^^^^^

int main()

//...

编译器看不到您放置在 main 之后的另一个函数声明。

char getLetter()
{
char letter = ' ';
cout << "Enter a character: ";
cin >> letter;
return letter;
} // end of getLetter function

所以编译器会报错。

很明显,您不会重载该函数并打错字。去掉main之前函数声明中的参数声明。

关于c++ - 在 Visual Studio 2015 中调用函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com