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;
}
主.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++书,以掌握编程基础知识。
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- std::is_base_of表示ctor编译错误
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- Qt Cmake 错误编译"GuiSupportQt not found"
- Opengl 精度转换错误编译错误 E0415
- 库将ARM架构错误编译为架构X64
- RT 音频 Mac 错误 g++ 编译错误
- 错误编译Boost.log
- 错误编译QT创建者 / QT窗口小部件示例
- 错误编译MIPS32
- Visual Studio 2013 中的错误(编译和运行代码)
- 链接错误编译qt项目在visual 2010
- 无法用模板错误编译nsgmls
- 奇怪的错误.编译失败
- 如果有人调用c++中的方法,则强制错误(编译时)
- 来自autoconf测试的错误编译命令
- 时间限制超出错误C++编译
- SFML 2.3 和 CodeBlocks 错误编译