C++代码编译错误

C++ code compiling error

本文关键字:错误 编译 代码 C++      更新时间:2023-10-16

>有谁知道为什么:

io.cpp:

# include <iostream>
int ReadNumber()
{
    using namespace std;
    cout << "Enter a number: ";
    int x;
    cin >> x;
    return x;
}
void WriteAnswer(int x)
{
    using namespace std;
    cout << "The answer is " << x << endl;
}

主.cpp:

int ReadNumber();
void WriteAnswer(int x);
int main()
{
    int x = ReadNumber();
    int y = ReadNumber();
    WriteAnswer(x+y);
    return 0;
}

在 Readnumber() 中没有 int x;main.cpp? 当我把 int x 放在括号里时,编译器说:"函数不接受 0 个参数"。

int ReadNumber();

main 中的此函数声明指示您的 ReadNumber 函数不带任何参数,并将返回一个 int。

如果在 ReadNumber 声明(在 main.cpp 中)和定义(在 io.cpp 中)中添加 int x 作为参数:

int ReadNumber(int x)

然后,对此函数的函数调用必须包含一个整数作为参数。这就是为什么您收到消息"函数不带 0 个参数":您正在调用一个等待 1 个参数的函数,而您对函数的调用不包括任何参数。

下面是包含参数的 ReadNumber 函数调用示例:

int YourParamUsedInReadNumber = 0;    
int x = ReadNumber(YourParamUsedInReadNumber); 

正如评论中所建议的,在继续之前,您可能应该得到一本好C++书,以掌握编程基础知识。