C++;链接到单独的.cpp文件时会显示;多重定义“; 我对C++非常陌生,可以使用一些帮助。我正在尝试将文件my_help_fxns.cpp链接到my main.cpp文件,以便可以在main.cpp中使用这些函数,但是当我尝试链接时,我在my_help_fxns中的每个函数都会出现以下错误: C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\ccaPL79E.o:data_vars_class.cpp:(.text+0x0): multiple definition of `my_help_fxns::print_vector_items_int_type(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<int, std::allocator<int> >)'; C:\Users\Geoff\AppData\Local\Temp\cc0mRP1w.o:main.cpp:(.text+0x0): first defined here

C++;链接到单独的.cpp文件时会显示;多重定义“; 我对C++非常陌生,可以使用一些帮助。我正在尝试将文件my_help_fxns.cpp链接到my main.cpp文件,以便可以在main.cpp中使用这些函数,但是当我尝试链接时,我在my_help_fxns中的每个函数都会出现以下错误: C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\ccaPL79E.o:data_vars_class.cpp:(.text+0x0): multiple definition of `my_help_fxns::print_vector_items_int_type(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<int, std::allocator<int> >)'; C:\Users\Geoff\AppData\Local\Temp\cc0mRP1w.o:main.cpp:(.text+0x0): first defined here,c++,C++,谢谢你的帮助 不要在其他cpp文件中包含my\u help\u fxns.cpp,因为这将有效地在所有cpp文件中定义这些函数。这违反了“一个定义”规则 反而 创建声明(但不定义)这些函数的头文件 将该头文件包括在所有CPP文件中 将my\u help\u fxns.cpp添加到编译命令行 按说明更改文件: main.cpp 此处给出: Enter to continue... sdfsdfsdfsd // --- INPUT 您最小化代码的方式并不好。我们永远不会知道编译器在哪里发生

谢谢你的帮助

不要在其他cpp文件中包含
my\u help\u fxns.cpp
,因为这将有效地在所有cpp文件中定义这些函数。这违反了“一个定义”规则

反而

  • 创建声明(但不定义)这些函数的头文件
  • 将该头文件包括在所有CPP文件中
  • my\u help\u fxns.cpp
    添加到编译命令行

按说明更改文件:

main.cpp 此处给出:

Enter to continue...
sdfsdfsdfsd    // --- INPUT

您最小化代码的方式并不好。我们永远不会知道编译器在哪里发生冲突,因为在任何地方都没有定义函数。此外,您不能使用.cpp扩展名将其中的代码作为头文件(在
data\u vars\u class.cpp
中给出)来实现。我添加了“pause\u program”函数,我正试图在my\u help\u fxns.cpp中调用它。我不知道你所说的“将其视为标题”是什么意思,我将如何实现?感谢不要使用
.cpp
文件包含

#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>

class DataVars
{
    private:
         ...
    public:
         ...
}
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>

#include "data_vars_class.hpp"
#include "my_help_fxns.cpp"

...i can use my_help_fxns here with no problem, as an instance of this class is created before main() in main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

namespace my_help_fxns
{
    void pause_program() {
        std::string dummy;
        std::cout << "Enter to continue..." << std::endl;
        std::getline(std::cin, dummy);            
    }
}
g++ main.cpp data_vars_class.cpp -o a.out
#include "data_vars_class.hpp"
#include <iostream>
#include <chrono>
#include "my_help_fxns.hpp" // change file extension from cpp -> hpp

DataVars dataVars;

int main () {

    my_help_fxns::pause_program();

    return 0; 
}
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>

#include "data_vars_class.hpp"
// #include "my_help_fxns.cpp" --> Not required here
g++ -o a.out main.cpp; ./a.out
Enter to continue...
sdfsdfsdfsd    // --- INPUT