C ++代码编译错误 [英] C++ code compiling error
问题描述
有人知道为什么在
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;
}
main.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个参数'
there is no int x in the Readnumber(); forward declaration in main.cpp? when I do put int x inside the brackets, the compiler says that: ''function does not take 0 arguments''
推荐答案
int ReadNumber();
这个函数声明在main中表示你的ReadNumber函数没有参数,
this function declaration in main indicate that your ReadNumber function takes no parameters and will return an int.
如果您在ReadNumber声明(在main.cpp中)和定义(在io.cpp中)中添加int x作为参数:
If you add int x as parameter in ReadNumber declaration (in main.cpp) and definition (in io.cpp) :
int ReadNumber(int x)
函数调用此函数必须包含一个整数作为参数。这就是为什么你得到的消息''函数不接受0参数'':你正在调用一个函数等待1参数,你的函数不调用包括任何。
your function calls to this function must then include an integer as parameter. That is why you get the message ''function does not take 0 arguments'' : you are calling a function that awaits 1 parameter and yours calls to the function doesnt include any.
下面是一个包含参数的ReadNumber函数调用示例:
Here is an example of ReadNumber function call including a parameter:
int YourParamUsedInReadNumber = 0;
int x = ReadNumber(YourParamUsedInReadNumber);
如注释中所建议的,你应该得到一个好的C ++书,之后再继续。
As suggested in comments, you should probably get a good C++ book to get a good grip on programming basics before going further.
这篇关于C ++代码编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!