Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Undefined reference to function in namespace

Here I am, trying to figure out what's wrong with my code without success :( I'm writing a resampler but I guess that's of no interest at all, I'm just trying yo make this stupid warning go away. Anyway, here's my code:

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

Compiling with g++ (linux) I get the following error:

$ 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

I'm going out of my mind, please help me! What am I doing wrong? Besides, If I remove ddc:: from the main function, gcc suggests me this:

main.cpp:59:49: note: suggested alternative:
ddc.hpp:24:7: note:   ‘ddc::perform_resampling’
like image 337
mr_hyde Avatar asked May 22 '13 18:05

mr_hyde


3 Answers

You declare a function taking a vector by value as its first argument, then define it taking the vector by reference. This produces a separate overload, and the declared function has no definition. Presumably it should be a reference, so add & to the declaration in the header.

You would get a more useful compiler error if you defined functions outside their namespace:

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

since it's an error to define a function with a qualified name if it hasn't been declared.

like image 80
Mike Seymour Avatar answered Sep 28 '22 01:09

Mike Seymour


These two are different:

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. This shows one good reason to put parameter names in your prototypes, even though they aren't strictly required. With parameter names, you can compare a prototype directly to a definition, and they should match character for character

like image 33
abelenky Avatar answered Sep 28 '22 01:09

abelenky


In your prototype, you are missing &

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

It appears in the definition

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);
}
like image 36
tay10r Avatar answered Sep 28 '22 00:09

tay10r