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

标签 c++ function compiler-errors declaration

更新:好的,谢谢大家!我的教科书说函数原型(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/

相关文章:

c++ - 调试时看不到任何变量值

c++ - QML 与 C++ 通信

c++ - 错误:非静态数据成员声明了 ‘auto’

javascript - 在ReactJS中如何在组件之前加载数据?

c++ - 错误: Invalid preproccessing directive #i did you mean #if?

c++ - 如何修复 "PI recursive function"代码以使用 n 的所有值?

c++ - 一次打印多个相等键的值 C++

python - 分配全局之前引用的局部变量不起作用

c++ - CMake : library not found

c++ - 接收错误 "no matching function for call to ' getline(std::istream&, int&, char )'"