编译简单的 C++ 应用程序时出错
Error with compiling simple C++ application
我有一个语法正确且非常简单的应用程序,它从用户那里获取两个整数并对它们进行减法和加法。但是当我尝试编译程序时,出现了这个错误:
Could not find 'C:\Users\MyUsername\source\repos\SimpleCalculator\SimpleCalculator\Debug\SimpleCalculator.obj'. SimpleCalculator.exe was built with /DEBUG:FASTLINK which requires object files for debugging.
这是我的代码:
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
int a, b;
cout << "Welcome." << endl;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
cout << "A+B is: ";
cout << addition(a,b) << endl;
cout << "A-B is: ";
cout << substraction(a,b) << endl;
system("pause");
}
int addition(int a, int b) {
return a + b;
}
int substraction(int a, int b) {
return a - b;
}
这个错误只发生在我有函数的时候。当我的代码是这样的:
cout << "A+B is: ";
cout << a+b << endl;
没有错误。
C++ 编译器从头到尾读取您的代码。在声明之前它不知道 addition
的存在 - 试图在声明之前使用它是错误的。
要修复您的编译器错误,请将 addition
和 substraction
的定义移动到 main
上方。
你需要在调用它们之前声明函数,所以你基本上有 2 个选择:
在main
之前声明它们:
int addition(int a, int b);
int substraction(int a, int b);
或者将它们的整个定义移到main
.
前面
在 C++(和 C99、C11 但不是旧的 C 标准)中,您必须先声明函数才能使用它们。这应该与定义相同,但没有正文 ({}
),就像您在头文件中找到的那样。
int addition(int a, int b);
int substraction(int a, int b);
void main()
{
...
只需在使用函数之前声明函数,如下所示:
int addition(int a, int b);
int substraction(int a, int b);
你会没事的。
或者,将函数的定义(实现)移动到之前 main()
。
PS:What should main() return in C and C++? 一个 int
,而不是 void
。
我有一个语法正确且非常简单的应用程序,它从用户那里获取两个整数并对它们进行减法和加法。但是当我尝试编译程序时,出现了这个错误:
Could not find 'C:\Users\MyUsername\source\repos\SimpleCalculator\SimpleCalculator\Debug\SimpleCalculator.obj'. SimpleCalculator.exe was built with /DEBUG:FASTLINK which requires object files for debugging.
这是我的代码:
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
int a, b;
cout << "Welcome." << endl;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
cout << "A+B is: ";
cout << addition(a,b) << endl;
cout << "A-B is: ";
cout << substraction(a,b) << endl;
system("pause");
}
int addition(int a, int b) {
return a + b;
}
int substraction(int a, int b) {
return a - b;
}
这个错误只发生在我有函数的时候。当我的代码是这样的:
cout << "A+B is: ";
cout << a+b << endl;
没有错误。
C++ 编译器从头到尾读取您的代码。在声明之前它不知道 addition
的存在 - 试图在声明之前使用它是错误的。
要修复您的编译器错误,请将 addition
和 substraction
的定义移动到 main
上方。
你需要在调用它们之前声明函数,所以你基本上有 2 个选择:
在main
之前声明它们:
int addition(int a, int b);
int substraction(int a, int b);
或者将它们的整个定义移到main
.
在 C++(和 C99、C11 但不是旧的 C 标准)中,您必须先声明函数才能使用它们。这应该与定义相同,但没有正文 ({}
),就像您在头文件中找到的那样。
int addition(int a, int b);
int substraction(int a, int b);
void main()
{
...
只需在使用函数之前声明函数,如下所示:
int addition(int a, int b);
int substraction(int a, int b);
你会没事的。
或者,将函数的定义(实现)移动到之前 main()
。
PS:What should main() return in C and C++? 一个 int
,而不是 void
。