简单的计算器应该需要2秒钟来修复它的10行

Simple calculator should take 2 seconds to fix its like 10 lines

本文关键字:10行 计算器 简单 2秒      更新时间:2023-10-16

试图制作一个简单的计算器,但我甚至无法让我的第一个函数工作。我一直在努力让我的头和.cpp尽可能地有条理,因为我记得以前这些都很重要。哈哈。我的印象是头文件包含在main.cpp中,它有头保护。这些只是我函数的声明,对吗?然后,Calculation的sFunctions.cpp就是我为上一个在头文件中创建的声明函数编写代码的地方。我也不确定什么时候应该包括iostream和stdafx.h等等。无论如何,提前感谢你们的帮助,这是我的3个文件,我现在只想从用户那里得到一个整数。

Calculator.cpp
#include "stdafx.h"
#include <iostream>
#include "CalculatorDeclarations.h"
int main()
{
    int getFirstInteger(int userInput)
    return 0;
}
CalculatorDeclarations.h
#ifndef ADD_H
#define ADD_H
#include "stdafx.h"
#include <iostream>
int getFirstInteger();
#endif
CalculationsFunctions.cpp
#include "stdafx.h"
#include <iostream>
int getFirstInteger(int userInput)
{
    std::cout << "Please enter the first integer you would like to use." <<     std::endl;
    std::cin >> userInput;
    return userInput;

}

错误:

Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) ConsoleApplication1 c:UsersShanedocumentsvisual studio 2015ProjectsConsoleApplication1ConsoleApplication1MSVCRTD.lib(exe_main.obj) ‌​1
Error LNK1120 1 unresolved externals ConsoleApplication1 c:usersshanedocumentsvisual studio 2015ProjectsConsoleApplication1DebugConsoleApplication1.exe 1 

好吧,你的第一个问题是你的"主要"方法

int main()
{
    int getFirstInteger(int userInput)
    return 0;
}

语法不正确(应以分号结尾)。你应该传递一个int。所以它可以被修改为这个

int main()
{
    int j = 1;
    getFirstInteger(j)
    return 0;
}

但值得一看这里的实现。您正在该方法中获得用户输入,因此不需要传递任何内容。而且您没有使用输出,所以不需要返回任何内容。

此外,您还包含了#include<iostream>。这在小程序中不会是什么大问题,但这是一种浪费和糟糕的做法。只在需要的地方包括它(在CalculationsFunctions.cpp中)。不要认为你需要#include "stdafx.h"