gpt4 book ai didi

c++ - iostream 头文件是否仅包含声明?

转载 作者:行者123 更新时间:2023-11-27 23:42:17 31 4
gpt4 key购买 nike

我正在学习 C++ 编程。我正在编写一个程序来实现计算器功能:

可以看出,我包含了 iostream 头文件。

#include <iostream>
#include "calculator.h"
int main()
{
//This program is meant to mimic the functionality of a calculator which
//support basic arithmetic operations.

//we will ask the user to input the desired operation and operands
getUserInput();

//We perform the mathematical operation and return the result
computeResult();

//We will print the output to the screen
printResult();

return 0;
}

现在我正在为 getUserInput 函数编写一个单独的 cpp 文件。

#include<iostream>
int getUserInput()
{
int a;
std::cout << "enter your input " << std::endl;
std::cin >> a;
return (a);

}

这里我也包括了 iostream。这样做可以吗?

Because , I suspect if iostream contains definitions, this can lead to a linking error related to multiple defintions.

最佳答案

您是对的,包含全局名称定义的 header 不能包含在多个源文件中而不导致链接错误。标准 header 不会这样做。

关于c++ - iostream 头文件是否仅包含声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53598760/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com