c++:命名空间中对函数的未定义引用

C++: Undefined reference to function in namespace

本文关键字:未定义 引用 函数 命名空间 c++      更新时间:2023-10-16

我在这里,试图找出我的代码没有成功的问题:我在写一个重采样器,但我想这没什么意思,我只是想让这个愚蠢的警告消失。总之,这是我的代码:

ddc.hpp

#ifndef __DIGITAL_DOWN_CONVERTER_H__
#define __DIGITAL_DOWN_CONVERTER_H__
#include <vector>
#include "interpolator.h"
namespace ddc {
    void decimate(std::vector<float> &, unsigned int);
    void expand(std::vector<float> &, unsigned int);
    void perform_resampling(std::vector<float>, unsigned int, unsigned int);
    void generate_filter(std::vector<float> &, unsigned int, unsigned int);
    float Sinc(float);
    unsigned int mcd(unsigned int, unsigned int);
}
#endif

ddc.cpp

#include "ddc.hpp"
namespace ddc {
    void perform_resampling(std::vector<float> &data, unsigned int freq_1, unsigned int freq_2) {
        unsigned int i, gcd = mcd(freq_1, freq_2);
        unsigned int downFactor, upFactor;
        std::vector<float> filter;
        downFactor = freq_1/gcd;
        upFactor   = freq_2/gcd;
        generate_filter(filter, 1024 /* lobi della semi-sinc */, upFactor);
        decimate(data, downFactor);
        expand(data, upFactor);
        interpolate_fft(data, filter);
    }
}

main.cpp

#include <vector>
#include "ddc.hpp"
using namespace std;
int main() {
    vector<float> data;
    // bla bla
    ddc::perform_resampling(data, 1000000, 60000);
    return 0;
}

用g++编译(linux)我得到以下错误:

$ make all
g++ -c ddc.cpp -o ddc.o -Wall -O3 -lm -m64
g++ -c main.cpp -o main.o -Wall -O3 -lm -m64
g++ ddc.o main.o -o ../bin/resampler
main.o: In function `main':
main.cpp:(.text.startup+0x255): undefine d reference to
`ddc::perform_resampling(std::vector<float, std::allocator<float> >, unsigned int, unsigned int)'
collect2: ld returned 1 exit status
make: *** [../bin/resampler] Error 1

我要疯了,请帮帮我!我做错了什么?此外,如果我从main函数中删除ddc::, gcc建议我这样做:

main.cpp:59:49: note: suggested alternative:
ddc.hpp:24:7: note:   ‘ddc::perform_resampling’

声明一个以值形式接受vector的函数作为其第一个实参,然后定义它以引用形式接受vector。这将产生一个单独的重载,并且声明的函数没有定义。假定它应该是一个引用,因此将&添加到头文件中的声明中。

如果你在命名空间之外定义函数,你会得到一个更有用的编译器错误:

void ddc::perform_resampling(std::vector<float> &data, unsigned int freq_1, unsigned int freq_2) {
//   ^^^^^
    // blah blah
}

,因为没有声明限定名的函数定义是错误的。

这两个是不同的:

void perform_resampling(std::vector<float> &data, unsigned int freq_1, unsigned int freq_2) 
void perform_resampling(std::vector<float> data, unsigned int freq_1, unsigned int freq_2) 

注:这显示了在原型中放置参数名的一个很好的理由,即使它们不是严格要求的。有了参数名,你可以直接比较原型和定义,它们应该匹配字符

在你的原型中,你缺少&

void perform_resampling(std::vector<float>, unsigned int, unsigned int);

出现在定义

void perform_resampling(std::vector<float> &data, unsigned int freq_1, unsigned int freq_2) {
    unsigned int i, gcd = mcd(freq_1, freq_2);
    unsigned int downFactor, upFactor;
    std::vector<float> filter;
    downFactor = freq_1/gcd;
    upFactor   = freq_2/gcd;
    generate_filter(filter, 1024 /* lobi della semi-sinc */, upFactor);
    decimate(data, downFactor);
    expand(data, upFactor);
    interpolate_fft(data, filter);
}