将函数添加到先前的链接文件后出现多重定义链接器错误

发布于 2024-09-07 00:55:31 字数 1167 浏览 11 评论 0原文

所以我的程序运行良好。编译、链接、运行,作品。然后,我决定向我的一个文件添加一个简单的函数,如下所示:

#ifndef UTILITY_HPP
#define UTILITY_HPP

/* #includes here.  There's no circular include, I've checked. */

namespace yarl
{
    namespace utility
    {
        (several function declarations and definitions)

        bool isVowel(const char c)
        {
            if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
                return true;
            else return false;
        }
    }
}

#endif

该函数定义是我对代码所做的唯一更改。其他一切都和原来一模一样。还没有什么叫它。我编译,但无法链接,g++ 为 #includes 这个文件的每个文件给出以下错误之一:

./obj/Feature.o: In function `yarl::utility::isVowel(char)':
/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src/Utility.hpp:130: multiple     definition of `yarl::utility::isVowel(char)'
./obj/Events.o:/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src    /Utility.hpp:130: first defined here
./obj/GameData.o: In function `yarl::utility::isVowel(char)':

如果我注释掉 isVowel,它会再次工作。我已经尝试过重命名,仍然不行。我尝试用 void randomFunctionName() {} 替换它,但仍然不起作用。我尝试将其设为非内联并将函数体放入 Utility.cpp 中,但仍然不起作用。我非常困惑。为什么添加一个简单的函数就会搞砸链接器?

So my program is working fine. Compiling, linking, running, the works. Then, I decide to add a simple function to one of my files, like this:

#ifndef UTILITY_HPP
#define UTILITY_HPP

/* #includes here.  There's no circular include, I've checked. */

namespace yarl
{
    namespace utility
    {
        (several function declarations and definitions)

        bool isVowel(const char c)
        {
            if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
                return true;
            else return false;
        }
    }
}

#endif

That function definition is the only change I've made to my code. Everything else is exactly the same as it was. Nothing calls it yet. I compile, and it fails to link, with g++ giving one of these errors for every file that #includes this one:

./obj/Feature.o: In function `yarl::utility::isVowel(char)':
/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src/Utility.hpp:130: multiple     definition of `yarl::utility::isVowel(char)'
./obj/Events.o:/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src    /Utility.hpp:130: first defined here
./obj/GameData.o: In function `yarl::utility::isVowel(char)':

If I comment out isVowel, it works again. I've tried renaming it, still doesn't work. I've tried replacing it with just void randomFunctionName() {}, still doesn't work. I've tried making it non-inline and putting the function body in Utility.cpp, still doesn't work. I am extremely confused. Why would adding one simple function screw up the linker?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来日方长 2024-09-14 00:55:31

要么声明内联函数,要么在单独的 .cpp 文件中定义它。否则,包含标头的每个 C++ 文件都会尝试创建自己的、公开可用的函数定义。

编辑:fwiw,如果您正在测试条件,则不需要显式返回 true 或 false。只需返回条件本身:

inline bool isVowel(const char c)
{
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

Either declare the function inline, or define it in a separate .cpp file. Otherwise every C++ file in which you include the header is trying to make its own, publicly-available definition of the function.

Edit: and fwiw, you don't need to explicitly return true or false if you're testing a conditional. Just return the conditional itself:

inline bool isVowel(const char c)
{
    return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文