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

标签 c++ header-files

我正在学习 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/

相关文章:

c++ - C++11 中的所有/大多数 setter 函数是否应该编写为接受通用引用的函数模板?

c++ - 在 C++ 代码中使用 "umlauts"

c++ - GCC 中的通用 lambda

c - 是否包含头文件

c++ - C++如何防止导入循环

c - 未定义类型的无效使用 ‘struct book_implementation’

c++ - 具有不同子类型的参数方法

c++ - WinAPI 将 "Edit"对话框写入管道(错误 : Stack around variable 'x' was corrupted)

c - 当库安装在/usr/include 中时,如何使 "common headers"工作?

c++ - 库的 .h 文件中出现 "{return 0;}"或 "{;}"的原因