c++ - 编译简单的C++应用程序时出错

标签 c++ function compiler-errors calculator

我有一个语法正确且非常简单的应用程序,该应用程序从用户那里获取两个整数,并对它们进行减法和加法运算。但是,当我尝试编译程序时,出现此错误:

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;

没有错误。

最佳答案

您需要在调用函数之前声明函数,因此基本上有2个选项:

main之前声明它们:

int addition(int a, int b);
int substraction(int a, int b);

或将它们的整个定义移到main前面。

关于c++ - 编译简单的C++应用程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46155484/

相关文章:

c++ - 什么时候应该从类方法返回对对象的引用

c++ - 对现有的 C 侵入式链表施加类型安全

python - Python 中的 match.arg 等价物?

python - 参数如何通过 __getattr__ 传递给函数

c++ - C++中的Const对象、Const成员函数和可变变量

C++成员初始化、复制初始化和默认初始化

xcode - 编译时没有OpenSSL支持错误

c++ - 变量引用以及指针如何与内存交互

c++ - 常量缓冲区中的纹理变换矩阵无法正常工作

visual-studio - 无法创建Xamarin.Forms应用程序