C++,错误 C2448 - 函数样式初始值设定项似乎是函数定义

标签 c++ visual-c++ compiler-errors

<分区>

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

(15): error C2065: 'x' : undeclared identifier

(15): error C2065: 'y' : undeclared identifier

(16): error C2448: 'writeanswer' : function-style initializer appears to be a function definition

(30): error C3861: 'writeanswer': identifier not found

这是我的代码:

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

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

编辑:我以为我已经试过了,我在这个问题上花了一整天!感谢所有的回答。

最佳答案

C++ 是强静态类型的,而不是动态类型的。您必须编写参数的类型 xy

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

void writeanswer( int x , int y )

关于C++,错误 C2448 - 函数样式初始值设定项似乎是函数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369636/

相关文章:

c++ - 存储不断访问的数据的最佳方式

c++ - 文件读取精度

visual-studio - 在cmake中指定pdb符号文件的路径

java - 编译代码时出现错误的操作数错误

java - 我试图从头开始在Java中实现链表,但出现以下错误

c++ - 从 map 访问,将字符串转换为 char*

java - 在c/c++中获取java对象的内存地址

visual-c++ - 不能在 MSVC 2010 中包含 Winsock2.h

c++ - 有没有办法让 Visual Studio 对包含的内容区分大小写?

C++调用类的模板函数作为模板参数