C++,错误C2448 - 函数式初始化器似乎是一个函数定义

Ram*_*ill 0 c++ compiler-errors visual-c++

该程序应该从用户获得两个输入并显示答案.这些是我得到的错误:

(15):错误C2065:'x':未声明的标识符

(15):错误C2065:'y':未声明的标识符

(16):错误C2448:'writeanswer':函数式初始值设定项似乎是一个函数定义

(30):错误C3861:'writeanswer':找不到标识符

这是我的代码:

#include "stdafx.h"
#include <iostream>
using namespace std;

int Readnumber()
{
    int num;
    cin >> num;
    return num;
}

    void writeanswer(x, y) //THIS IS LINE 15
{ //THIS IS LINE 16
    int a;
    a = x + y;
    cout << "This is the answer: " << a;
}


int main()
{
    cout << "Please enter a number: ";
    int x = Readnumber();

    cout << "Please enter another number: ";
    int y = Readnumber();

    writeanswer(x, y); //THIS IS LINE 30


    system("Pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我认为第30行的错误是导致更多错误的主要问题.我试过谷歌,我似乎无法解决它.

编辑:我以为我曾尝试过,我整天都在研究这个问题!感谢所有的答案.

Man*_*726 6

C++是强静态类型的,不是动态类型的.你必须写出参数的类型xy.

正如您a在函数体内指定局部变量的类型一样,您必须指定输入参数的类型,x并且y:

void writeanswer( int x , int y )
Run Code Online (Sandbox Code Playgroud)


xcd*_*n05 5

在 for 的函数声明中writeanswer,您有

void writeanswer(x, y)
Run Code Online (Sandbox Code Playgroud)

但应该是

void writeanswer(int x, int y)
Run Code Online (Sandbox Code Playgroud)

编译器不知道您的代码中 x 和 y 是什么类型。它不能暗示主函数的数据类型,因为它们的范围仅限于声明它们的主函数。

在某些语言中,包括 MATLAB 和 Python,您不需要总是指定数据类型,因为它是在编译/解释时根据您尝试将其设置为什么类型的值隐式排序的。C++ 不是那样工作的。C++ 在这方面非常严格,您必须在声明时指定每个数据类型。