小编Ale*_*anu的帖子

如何在多个 C++ 文件中使用 C header.h 而不会出现多个定义错误?

我想使用 Wireshark 开源项目中“func.h”文件中的函数。我需要在多个 .cpp 文件中使用 funct() 函数,但出现多重定义错误。

函数.h:

    #ifndef func_h
    #define func_h
    #include<string>
    
     void *funct(char *cName)
     {
         std::string name = cName;
         cName+= ".extension";
     }
Run Code Online (Sandbox Code Playgroud)

在 .cpp 文件中,我包含 func.h:

    #include "func.h"
Run Code Online (Sandbox Code Playgroud)

并从 2 个 .cpp 文件调用 funct() 函数:

    funct("program");
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能避免出现多重定义错误?解决方法是将函数定义复制并粘贴到每个 .cpp 文件中并更改函数名称,但这很丑陋。

非常感谢。

c++ compiler-errors header

1
推荐指数
1
解决办法
769
查看次数

标签 统计

c++ ×1

compiler-errors ×1

header ×1