1
我想這段代碼錯誤LNK2005同時實現升壓函數指針
demo.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);
};
demo.cpp
#include <iostream>
#include "demo.hpp"
void funcPointer::add_call(func f)
{
cout << "Result of add: " << f(5,7) <<endl;
}
的main.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;
}
編譯時我得到了f更正錯誤:
demo.obj : error LNK2005: "public: void __thiscall funcPointer::add_call(class boost::function<int __cdecl(int,int)>)" ([email protected]@@[email protected][email protected]@[email protected]@@Z) already defined in main.obj
E:\vs_c++\boost_func_ptr\Debug\boost_func_ptr.exe : fatal error LNK1169: one or more multiply defined symbols found
我不明白這是什麼樣的錯誤,有人可以幫我解決這個錯誤嗎?
Thanx Joachim,它的工作 – NIXIN