c++ - Why doesn't Visual Studio 2019 compile my code?
问题描述
I have been trying to get this code working. I have made this code mostly myself and google. I am very beginner in this, so I have no idea how to fix this.
I have tried cutting code, transforming it to c#,(modifying it of course) new file and different compiler.
#include <iostream>
using namespace std;
//suurin_luku.cpp
int question() {
double answers;
cout << "Do you want the biggest number, or smallest number?\n";
cout << "0 for biggers, 1 for smaller.\n";
cin >> answers;
if (answers == 0) {
int biggest();
}
if (answers == 1) {
int smallest();
}
return 0;
}
int biggest()
{
float input1, input2, input3;
cout << "Please insert three numbers.\n";
cin >> input1 >> input2 >> input3;
if (input1 >= input2 && input1 >= input3)
{
cout << "The largest number is: " << input1;
}
if (input2 >= input1 && input2 >= input3)
{
cout << "The largest number is: " << input2;
}
if (input3 >= input1 && input3 >= input2) {
cout << "The largest number is: " << input3;
}
return 0;
}
int smallest()
{
float input11, input22, input33;
cout << "Insert three numbers.";
cin >> input11 >> input22 >> input33;
if (input11 <= input22 && input11 <= input33)
{
cout << "The smallest number is: " << input11;
}
if (input22 <= input11 && input22 <= input33)
{
cout << "The smallest number is: " << input22;
}
if (input33 <= input11 && input33 <= input22) {
cout << "The smallest number is: " << input33;
}
return 0;
}
When user inputs 0, it shows the largest inputted number. When user inputs 1, it shows the smallest inputted number. Error codes are LNK1120 and LNK2019.
解决方案
如果这就是您的所有代码,那么您可能会收到链接错误,因为您缺少一个main
函数。如果我main
在我的 VS 项目中省略,我会得到这两个确切的链接错误。将此添加到您的代码中:
int main() {
question();
}
此外,您不是在调用函数,而是在声明它们:
if (answers == 0) {
int biggest();
}
if (answers == 1) {
int smallest();
}
删除那些int
调用函数。您必须将int question()
其他两个函数放在下面,否则它将找不到它们,除非您事先声明它们,如下所示:
int biggest(); // declaring them so question() knows their signature
int smallest();
int question() { ... }; // question() calls biggest() and smallest()
int biggest() { ... }; // actual implementation of those functions
int smallest() { ... };
int main { ... }
推荐阅读
- javascript - 如何在节点 js(nodemailer)中不提供我的电子邮件 ID 和密码的情况下发送电子邮件?
- http - 通过网络将内容发送到客户端,而客户端无法将内容提取到其本地计算机
- docker - Kubernetes 错误工作者绑定端口 8080 上的 Presto 集群
- c# - 如何用正则表达式替换字符串以查找“:”和可变空格?
- python - Pyinstaller 和 Tesseract OCR
- python-3.x - pyqt5 QMdiSubWIdow 关闭事件时隐藏
- .net - 如何从 VB.net 中的 JSON 键值对中的特定键获取值作为字符串?
- swift - how to properly use a protocol that has an associated type to describe a property in another object
- javascript - 使用 jQuery 将 XML 转换为 JSON 并输出为 HTML
- python - 如果满足不同的条件,有没有办法将文本位写入不同的文件?