为啥在vs中我在头文件中设置了一个文件。在源文件中#include包含。当编译源文件时老说错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为啥在vs中我在头文件中设置了一个文件。在源文件中#include包含。当编译源文件时老说错误相关的知识,希望对你有一定的参考价值。

你不能包含txt文件啊,编译器不识别的。必须是头文件或.c/.cpp或其它类似.c/.cpp的文件。但必须是可编译的! 参考技术A 没有txt这种库。。追问

为什么?不是是在头文件中新建一个

参考技术B 库名必须是.h

为啥在头文件中声明并在文件中定义会给出多个定义错误?

【中文标题】为啥在头文件中声明并在文件中定义会给出多个定义错误?【英文标题】:Why does Declaring in header file and defining in file gives multiple definition error?为什么在头文件中声明并在文件中定义会给出多个定义错误? 【发布时间】:2016-11-13 13:27:50 【问题描述】:

我是 C++ 编程新手。我对JAVA有一些更好的了解。所以使用hackerrank我正在尝试学习C++。为了跟踪每个程序一个单独的实体,我开始为每个程序或挑战使用头文件和程序文件。所以我正在尝试进行hackerrank练习输入和输出 (https://www.hackerrank.com/challenges/cpp-input-and-output)。所以我尝试以这种方式实现我的程序;

InputAndOutput.h

 #ifndef INPUTANDOUTPUT_H_
 #define INPUTANDOUTPUT_H_

int arr[3];
int m;
int InputAndOutput();

#endif

InputAndOutput.cpp

#include "InputAndOutput.h"
#include<iostream>
#include<cmath>
#include<cstdio>

int InputAndOutput()
     int arr[3];
     for(int i1 = 0; i1 < 3 ; i1++)
        std::cin >> arr[i1];
     for(int i = 0; i < 3 ; i++)
       m = m + arr[i];
     return m;

ma​​in.cpp

#include<iostream>
//#include<day1DataTypes>
#include<cmath>
#include<cstdio>
//#include "day1DataTypes.h"
#include "InputAndOutput.h"

int main()

    int k = InputAndOutput();   \\the error persists even the whole block is commented
    std::cout << k << std::endl ;

这个给出了以下错误;

Description Resource    Path    Location    Type
first defined here  Hackerrank      line 6  C/C++ Problem
first defined here  Hackerrank      line 8  C/C++ Problem
make: *** [Hackerrank] Error 1  Hackerrank          C/C++ Problem
multiple definition of `arr'    Main.cpp    /Hackerrank line 9  C/C++ Problem
multiple definition of `m'  Main.cpp    /Hackerrank line 12 C/C++ Problem

请解释一下这个符号有什么问题。顺便说一句,我正在使用 eclipse,它在编译时抛出错误。

【问题讨论】:

【参考方案1】:

先解释最简单的问题,我们来看看“int arr[3];”

对于那个变量声明,它在 InputAndOutput.h 头文件中声明和实现。

main.cpp 和 InputAndOutput.cpp 都包含头文件,因此实现了两次变量。

要声明可以在其他文件中使用的变量,您可以使用:

InputAndOutput.h

extern int arr[3];
extern int m;

InputAndOutput.cpp

int arr[3];
int m;

这告诉编译器有 2 个变量 arr 和 m,它们在 .h 文件中声明,但在外部文件中使用 extern 关键字实现。

请注意,您在问题中发布的代码只是 C++ 文件中的 C。

在 C++ 中,不鼓励使用全局变量来存储数据。

因此,如果您要删除全局变量并使用 c++ stl 容器,您将拥有以下内容:

InputAndOutput.h

#include <array>
int32_t InputAndOutput(std::array<int32_t, 3>& arr, int32_t& m);

InputAndOutput.cpp

int32_t InputAndOutput(std::array<int32_t, 3>& arr, int32_t& m)
    
     for(auto i1 = 0; i1 < 3 ; i1++)
         std::cin >> arr[i1];
     for(auto i = 0; i < 3 ; i++)
         m = m + arr[i];
     return m;
 

ma​​in.cpp

int main()

    auto arr = std::array<int32_t, 3>0,0,0;
    auto m = 0;
    const auto k = InputAndOutput(arr, m);
    std::cout << k << std::endl ;

现在,这应该可以解决您的大部分问题,但是,我没有在您的原始代码中看到您是如何从 std::cin 获取输入的,因为您没有提示用户输入......并且这会导致错误。

既然您正在学习 C++,那么您应该学习 Modern C++ 而不是 C++98。

我建议您阅读https://github.com/isocpp/CppCoreGuidelines

另外,还可以查看 Herb Sutter 的网站,了解关于几乎始终自动的问题,地址为 https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/

【讨论】:

以上是关于为啥在vs中我在头文件中设置了一个文件。在源文件中#include包含。当编译源文件时老说错误的主要内容,如果未能解决你的问题,请参考以下文章

为啥在头文件中声明并在文件中定义会给出多个定义错误?

WIN32中我新建一个cpp和.h 在头文件中写了一个结构体然后在.cpp文件中使用该结构体总是报错具体如下图

如何在 VS2015 中调试 Clang 静态链接库

Intellij Idea中设置了忽略提交iml文件,但不起作用

为啥我的 django 模板无法识别我在 views.py 文件中设置的 url 参数?

在 UIScrollview 中设置 size 属性