Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++;:从main()调用模板化函数_C++_Templates_Gcc_Undefined Reference - Fatal编程技术网

C++ C++;:从main()调用模板化函数

C++ C++;:从main()调用模板化函数,c++,templates,gcc,undefined-reference,C++,Templates,Gcc,Undefined Reference,这是我第一次尝试在函数中使用模板,但似乎无法使它们正常工作。我在一个名为ddc.hpp #ifndef __DIGITAL_DOWN_CONVERTER_H__ #define __DIGITAL_DOWN_CONVERTER_H__ namespace ddc { template <class T> void perform_resampling(std::vector<T> &, unsigned int, unsigned int); } #end

这是我第一次尝试在函数中使用模板,但似乎无法使它们正常工作。我在一个名为
ddc.hpp

#ifndef __DIGITAL_DOWN_CONVERTER_H__
#define __DIGITAL_DOWN_CONVERTER_H__
namespace ddc {
    template <class T> void perform_resampling(std::vector<T> &, unsigned int, unsigned int);
}
#endif
#include "ddc.hpp"
template <class T>
void ddc::perform_resampling(std::vector<T> &data, unsigned int f1, unsigned int f2) {
    // do stuff
}
这是我的
main.cpp

#include "ddc.hpp"
int main() {
    std::vector<float> v (100000);
    ddc::perform_resampling(v, 1000, 10);

    return 0;
}
#包括“ddc.hpp”
int main(){
std::向量v(100000);
ddc::执行_重采样(v,1000,10);
返回0;
}
使用gcc(linux)编译时出现以下错误:

$ 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+0xed): undefined reference to `void ddc::perform_resampling<float>(std::vector<float, std::allocator<float> >&, unsigned int, unsigned int)'
collect2: ld returned 1 exit status
make: *** [../bin/HW_3] Error 1
$g++-cddc.cpp-odc.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:在函数“main”中:
main.cpp:(.text.startup+0xed):对“void ddc::perform_resampling(std::vector&,unsigned int,unsigned int)”的未定义引用
collect2:ld返回了1个退出状态
make:**[../bin/HW_3]错误1

我做错了什么吗?

您也需要将模板实现放在头文件中。

您也需要将模板实现放在头文件中。

模板定义需要与声明一起使用,所以所有内容都需要放在头文件中。

模板定义需要与声明一起使用,因此,所有内容都需要在头文件中。

您需要将模板函数的定义放置在使用它的代码可见的位置,或者使用显式模板实例化来确保生成函数的代码

如果您不想公开
perform_resampling
的实现,您仍然可以强制编译器显式地为其生成代码。当放置在ddc.cpp中时,下一行将指示编译器生成代码,以
向量作为其第一个参数执行重采样

template void ddc::perform_resampling(std::vector<float> &data, unsigned int f1, unsigned int f2);
template void ddc::执行_重采样(std::vector&data、unsigned int f1、unsigned int f2);

您需要将模板函数的定义放置在使用它的代码可见的位置,或者使用显式模板实例化来确保生成函数的代码

如果您不想公开
perform_resampling
的实现,您仍然可以强制编译器显式地为其生成代码。当放置在ddc.cpp中时,下一行将指示编译器生成代码,以
向量作为其第一个参数执行重采样

template void ddc::perform_resampling(std::vector<float> &data, unsigned int f1, unsigned int f2);
template void ddc::执行_重采样(std::vector&data、unsigned int f1、unsigned int f2);

你不能像对待函数和方法那样将接口与定义分开,这很简单,你需要在一个地方声明和定义一个模板,如果你愿意阅读关于模板的内容,原因很清楚,但简而言之,你的第一段代码对编译器来说没有意义,而且它缺少模板定义。你不能像对待函数和方法那样将接口与定义分开,很简单,你需要在一个地方声明和定义模板,如果您愿意阅读有关模板的内容,原因很清楚,但简而言之,您的第一段代码对编译器来说没有意义,并且缺少模板定义。