错误:(functionName)已经在Algorithms.obj中定义
Error: (functionName) already defined in 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);
}
你不能在头文件中这样定义函数。将其标记为static
或inline
,否则它将在包含头文件的所有源文件中定义。
如果您想使用来自多个源文件的函数,只需在头文件中声明:
bool BPredicate(const string& a, const string& b);
(注意分号,没有函数体)
然后在一个源文件中定义函数
相关文章:
- 如何在.obj文件上运行IlDasm?
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 在 opengl 中渲染 obj 文件时出现黑窗口
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- 正在读取 obj 文件!(指数)
- 我在主函数的左括号上不断收到错误,消息为obj\Debug\main.o||在函数"ZN11linked_listC1Ev"中:|
- float* 已在 Gameobject.obj 中定义
- 可执行文件C++包括.obj,.lib和.dll吗?
- C++成功复制动态分配的 obj 而不复制 ctor?
- OBJ 文件C++与 ubuntu 问题
- 为什么定义复制构造函数会给我错误:无法将类型 'obj&' 的非常量左值引用绑定到类型为"obj"的右值?
- OBJ 向量到成员函数
- 在istream_iterator中使用后的 C++ FSTREAM obj 回滚
- 使用sscaf_s从obj读取顶点线不起作用
- 移植DXUT SDKMesh以导入OBJ文件,在哪里添加纹理坐标和索引
- /usr/bin/ld: build-debug/obj/gpsitem.o: 文件无法识别: 文件被截断
- 将多个网格放入单个波前 OBJ 文件中
- :-1: 错误: LNK1104: 无法打开文件"我的项目.obj"
- Python 脚本到 c .obj 文件
- 错误:(functionName)已经在Algorithms.obj中定义