错误:(functionName) 已在 Algorithms.obj 中定义
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在标头中定义这样的函数。将其标记为
static
或inline
,或者它将在包含标头的所有源文件中定义。如果您想使用多个源文件中的函数,只需在标头中声明即可:(
注意分号,并且没有函数体。)
然后在其中一个源文件中定义该函数。
You can not define functions in headers like that. Either mark it as
static
orinline
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:
(Note the semicolon and no function body.)
Then define the function in one of your source files.