其他分享
首页 > 其他分享> > C:对命名空间中的函数的未定义引用

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

作者:互联网

我在这里,试图找出我的代码有什么问题但没有成功:(
我正在写一个重新采样器,但我想这根本没什么兴趣,我只是想让你这个愚蠢的警告消失.无论如何,这是我的代码:

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’

解决方法:

你声明一个函数将一个矢量值作为它的第一个参数,然后通过引用定义它.这会产生一个单独的重载,并且声明的函数没有定义.据推测它应该是一个参考,所以添加&到标题中的声明.

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

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

因为如果尚未声明,则定义具有限定名称的函数是错误的.

标签:c,g,undefined-reference
来源: https://codeday.me/bug/20190823/1695978.html