实现提升函数指针时出错LNK2005
error LNK2005 while implementing boost function pointer
我正在尝试这段代码
演示.hpp
#include <boost/function.hpp>
#include <boost/bind.hpp>
using namespace std;
typedef boost::function<int(int,int)>func;
class funcPointer
{
public:
void add_call(func);
};
演示.cpp
#include <iostream>
#include "demo.hpp"
void funcPointer::add_call(func f)
{
cout << "Result of add: " << f(5,7) <<endl;
}
主.cpp
#include "demo.cpp"
int add(int x,int y)
{
cout << "x: " << x <<endl;
cout << "y: " << y <<endl;
return x + y;
}
int main()
{
funcPointer *fun = new funcPointer;
fun->add_call(boost::bind(add, _1, _2));
return 0;
}
编译时出现以下错误:
demo.obj : error LNK2005: "public: void __thiscall funcPointer::add_call(class boost::function<int __cdecl(int,int)>)" (?add_call@funcPointer@@QAEXV?$function@$$A6AHHH@Z@boost@@@Z) already defined in main.obj
E:vs_c++boost_func_ptrDebugboost_func_ptr.exe : fatal error LNK1169: one or more multiply defined symbols found
我不明白这是什么错误,有人可以帮我解决这个错误吗?
不要#include
源文件!
在您的情况下(我只是在这里猜测),文件demo.cpp
是项目的一部分,因此它被编译并链接以创建可执行文件。问题是,由于您还将该源文件作为头文件包含在内,因此该函数也在main.cpp
中定义。
在main.cpp
中,您应该包含头文件demo.hpp
。
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- LNK2005在.hpp文件中声明函数时出错
- 实现提升函数指针时出错LNK2005
- Visual Studio链接器在尝试使用DLL中的静态库时出错(LNK2038、LNK2005)