编译并执行以下文件没有任何问题。当我从 Ode.cpp 中的aos::
函数前面删除euler
时,编译器会抛出以下错误。
Ode.cpp:13: undefined reference to `aos::euler(std::vector<double, std::allocator<double> >* (*)(std::vector<double, std::allocator<double> >*, unsigned int, unsigned int), std::vector<double, std::allocator<double> >*, unsigned int, unsigned int)'
collect2: error: ld returned 1 exit status
为什么这样做?我已经在Ode的定义和实现文件中定义了namespace
。
干杯, MrMcKizzle
#include <SDL2/SDL.h>
#include <vector>
#include <iostream>
#include <string>
#ifndef ODE_HPP
#define ODE_HPP
namespace aos
{
std::vector< double > * euler(
std::vector< double > * (*)(std::vector< double > *, Uint32, Uint32),
std::vector< double > * x, Uint32 dt, Uint32 t
);
class Integrator {
public:
Integrator();
//~Integrator();
std::vector< double > * (*integrator)(
std::vector< double > * (*)(std::vector< double > *, Uint32, Uint32),
std::vector< double > * x, Uint32 dt, Uint32 t) = euler; ///< Stores a pointer to the default ODE integrator function (defaults to euler)
std::vector< double > * integrate(
std::vector< double > * (*)(std::vector< double > *, Uint32, Uint32),
std::vector< double > * x, Uint32 dt, Uint32 t
);
};
}
#endif
using namespace aos;
std::vector< double > * aos::euler(
std::vector< double > * (*f)(std::vector< double > *, Uint32, Uint32),
std::vector< double > * x, Uint32 dt, Uint32 t)
{
std::cout << "aos::euler" << std::endl;
return f(x, dt, t);
}
Integrator::Integrator(){}
std::vector< double > * Integrator::integrate(
std::vector< double > * (*f)(std::vector< double > *, Uint32, Uint32),
std::vector< double > * x, Uint32 dt, Uint32 t)
{
std::cout << "Integrator::integrate" << std::endl;
return this->integrator(f, x, dt, t);
}
#include "Ode.hpp"
#include <SDL2/SDL.h>
#include <vector>
#include <iostream>
#include <string>
std::vector< double > * dummysys(std::vector< double > * x, Uint32 dt, Uint32 t)
{
cout << "main.cpp::dummysys" << endl;
return new vector< double >();
}
int main(int argc, char **argv)
{
aos::Integrator * intgr = new aos::Integrator();
vector< double > x;
//integrate(dummysys, &x, 16, 123);
intgr->integrate(dummysys, & x, 16, 123);
return 0;
}
答案 0 :(得分:1)
在您的cpp中,将您的实现包含在命名空间aos中,就像在hpp。
中一样namespace aos
{
std::vector< double > * euler(
std::vector< double > * (*f)(std::vector< double > *, Uint32, Uint32),
std::vector< double > * x, Uint32 dt, Uint32 t)
{
std::cout << "aos::euler" << std::endl;
return f(x, dt, t);
}
Integrator::Integrator(){}
std::vector< double > * Integrator::integrate(
std::vector< double > * (*f)(std::vector< double > *, Uint32, Uint32),
std::vector< double > * x, Uint32 dt, Uint32 t)
{
std::cout << "Integrator::integrate" << std::endl;
return this->integrator(f, x, dt, t);
}
}
您可以使用main.cpp中的using namespace aos