错误:(functionName) 已在 Algorithms.obj 中定义

发布于 2024-12-17 13:05:19 字数 1925 浏览 0 评论 0原文

1>GameWinMain.obj : error LNK2005: "bool __cdecl BPredicate(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?BPredicate@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) already defined in Algorithms.obj
1>C:\Algorithms.exe : fatal error LNK1169: one or more multiply defined symbols found

当我尝试在标头中声明函数时,我得到了后者。 我不知道是什么原因造成的,一切都包括警卫。 有趣的是:如果我将标头中的函数定义为内联函数,它就会编译。 有人可以帮忙吗?

请参阅下面的代码:

Algorithms.h

#pragma once

//...other code

bool BPredicate(const string& a, const string& b){
    string::const_iterator it;
    UINT numA = 0;
    UINT numB = 0;
    for (it = a.begin(); it != a.end(); ++it) {
        if((*it) == ' ') {
            if (*(it-1) != ' ') {
                ++numA;
            }
        }
    }
    for (it = b.begin(); it != b.end(); ++it) {
        if((*it) == ' ') {
            if (*(it-1) != ' ') {
                ++numB;
            }
        }
    }

    return (numA < numB);
}

GameWinMain.h

#pragma once

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow);

GameWinMain.cpp

#include "GameWinMain.h"
#include "GameEngine.h"
#include "Algorithms.h" 

#define GAME_ENGINE (GameEngine::GetSingleton())

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    if (GAME_ENGINE == NULL) return FALSE; 

    GAME_ENGINE->SetGame(new Algorithms()); 

    return GAME_ENGINE->Run(hInstance, iCmdShow); 
}
1>GameWinMain.obj : error LNK2005: "bool __cdecl BPredicate(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?BPredicate@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) already defined in Algorithms.obj
1>C:\Algorithms.exe : fatal error LNK1169: one or more multiply defined symbols found

I'm getting the latter when trying to declare a function in the header.
I'm not sure what's causing this, everything has include guards.
Funny thing is: if I define the function in the header as inline, it compiles.
Can anyone help?

See code Below:

Algorithms.h

#pragma once

//...other code

bool BPredicate(const string& a, const string& b){
    string::const_iterator it;
    UINT numA = 0;
    UINT numB = 0;
    for (it = a.begin(); it != a.end(); ++it) {
        if((*it) == ' ') {
            if (*(it-1) != ' ') {
                ++numA;
            }
        }
    }
    for (it = b.begin(); it != b.end(); ++it) {
        if((*it) == ' ') {
            if (*(it-1) != ' ') {
                ++numB;
            }
        }
    }

    return (numA < numB);
}

GameWinMain.h

#pragma once

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow);

GameWinMain.cpp

#include "GameWinMain.h"
#include "GameEngine.h"
#include "Algorithms.h" 

#define GAME_ENGINE (GameEngine::GetSingleton())

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    if (GAME_ENGINE == NULL) return FALSE; 

    GAME_ENGINE->SetGame(new Algorithms()); 

    return GAME_ENGINE->Run(hInstance, iCmdShow); 
}

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

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

发布评论

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

评论(1

孤独岁月 2024-12-24 13:05:19

您不能在标头中定义这样的函数。将其标记为staticinline,或者它将在包含标头的所有源文件中定义。

如果您想使用多个源文件中的函数,只需在标头中声明即可:(

bool BPredicate(const string& a, const string& b);

注意分号,并且没有函数体。)

然后在其中一个源文件中定义该函数。

You can not define functions in headers like that. Either mark it as static or inline or it will be defined in all source files where you include the header.

If you want to use the function from several source files, only declare it in the header:

bool BPredicate(const string& a, const string& b);

(Note the semicolon and no function body.)

Then define the function in one of your source files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文