Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么要重新定义函数名称空间?_C++_Linker_Namespaces - Fatal编程技术网

C++ 为什么要重新定义函数名称空间?

C++ 为什么要重新定义函数名称空间?,c++,linker,namespaces,C++,Linker,Namespaces,编译和执行以下文件没有任何问题。当我从Ode.cpp中的euler函数前面删除aos::时,编译器抛出以下错误 Ode.cpp:13: undefined reference to `aos::euler(std::vector<double, std::allocator<double> >* (*)(std::vector<double, std::allocator<double> >*, unsigned int, unsigned int

编译和执行以下文件没有任何问题。当我从Ode.cpp中的
euler
函数前面删除
aos::
时,编译器抛出以下错误

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.cpp
使用名称空间aos;
std::vector*aos::euler(
标准::向量*(*f)(标准::向量*,Uint32,Uint32),
标准:向量*x,Uint32 dt,Uint32 t)
{ 
标准::cout*(*f)(标准::向量*,Uint32,Uint32),
标准:向量*x,Uint32 dt,Uint32 t)
{
std::cout*dummysys(std::vector*x,Uint32 dt,Uint32 t)
{
cout x;
//集成(dummysys和x,16123);
intgr->integrate(dummysys和x,16123);
返回0;
}

在您的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);
}
}
名称空间aos
{
标准::矢量*欧拉(
标准::向量*(*f)(标准::向量*,Uint32,Uint32),
标准:向量*x,Uint32 dt,Uint32 t)
{ 
标准::cout*(*f)(标准::向量*,Uint32,Uint32),
标准:向量*x,Uint32 dt,Uint32 t)
{

std::难道我不明白,Ode.hpp和Ode.cpp是一样的,只是hpp包含Ode.hpp?Woops!可以解决这个问题。谢谢你,事实是,当定义一个名称空间时,你将所有东西都包含在里面,使用名称空间是为了访问已经定义的方法、对象和变量。当然,这是规避这个问题的方法。我只是说这是我们在野外发现这些实现的通常方式。谢谢,这解决了问题。我错误地假设
使用名称空间aos;
名称空间aos{…}
都是一样的。除了第一个可以覆盖整个块之外。没关系,名称空间对于我破解一个包含40多个嵌套结构和内部嵌套结构的结构非常有用。多亏了它们,我可以在保持相同名称的同时解耦所有内容,而且原始的遗留实现仍然兼容。它们“除了封闭库之外,您还真的很有用。@McKizzle先生:我的建议是:不要使用指令进行编码(即避免
使用命名空间
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;
}
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);
}
}