首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:(functionName)已在Algorithms.obj中定义

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

Stack Overflow用户
提问于 2011-11-22 11:14:30
回答 1查看 552关注 0票数 0
代码语言:javascript
复制
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

当我试图在标头中声明一个函数时,我得到了后者。我不知道是什么导致了这一切,一切都包括警卫。有趣的是:如果我将标题中的函数定义为内联,它就会编译。有人能帮忙吗?

见下面的代码:

算法h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#pragma once

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

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

GameWinMain.cpp

代码语言:javascript
复制
#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); 
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-22 11:17:23

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

如果要使用多个源文件中的函数,只需在标题中声明该函数:

代码语言:javascript
复制
bool BPredicate(const string& a, const string& b);

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

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8225867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档