【问题标题】:C++: Undefined reference to function in namespaceC ++:对命名空间中函数的未定义引用
【发布时间】:2013-05-17 21:55:42
【问题描述】:

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

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);
    }
}

ma​​in.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

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

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

【问题讨论】:

  • 这可能是因为你有一个 & 在定义中而不是原型

标签: c++ g++ undefined-reference


【解决方案1】:

你声明一个函数,将一个向量作为它的第一个参数,然后定义它,将向量作为引用。这会产生一个单独的重载,并且声明的函数没有定义。想必应该是引用,所以在header的声明中加上&amp;

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

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

因为如果没有声明一个具有限定名称的函数是错误的。

【讨论】:

    【解决方案2】:

    这两个是不同的:

    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) 
    

    P.S. 这说明了将参数名称放在原型中的一个很好的理由,即使它们不是严格要求的。使用参数名称,您可以直接将原型与定义进行比较,并且它们应该逐字符匹配

    【讨论】:

    • 这就是为什么 Straustrup 建议在从命名空间定义某些内容时使用 namspace_name:: 而不是 namespace_name{}
    【解决方案3】:

    在您的原型中,您缺少 &

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-25
      • 1970-01-01
      • 2022-12-03
      • 2021-06-30
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多