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
#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);
}发布于 2011-11-22 11:17:23
您不能在这样的标题中定义函数。要么将其标记为static或inline,要么在包含头的所有源文件中定义它。
如果要使用多个源文件中的函数,只需在标题中声明该函数:
bool BPredicate(const string& a, const string& b);(注意分号,没有函数体。)
然后在一个源文件中定义函数。
https://stackoverflow.com/questions/8225867
复制相似问题